📘 CodingMarble Learn

Subprograms: Functions, Parameters and Logical Functions

A subprogram is a named block of code that does one job. You write it once and call it as many times as you like. It can take parameters (inputs). A procedure only does a job; a function also returns a value with return. A logical function returns True or False and can be used directly in an if or while.

🎬 Step-by-step story

  1. The problem. The same 4 lines are copied three times. 12 lines, and a mistake must be fixed in three places.
  2. The fix. Write the lines once and give them a name. Then call it: square(2), square(5), square(7).
  3. Parameters. The name in the definition is the parameter. The value in the call is the argument. Move the slider.
  4. Procedure or function. A procedure only does a job. A function returns a value to the caller.
  5. Logical function. It returns only True or False. is_even(7) is False, is_even(8) is True.
  6. Your turn. Pick square, maximum or is_even. Set a and b. Guess the result, then check.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why not just copy the lines again?

Copies need fixing in every place, and one is easy to forget. See the three towers: 12 lines.

How many times can I call a subprogram?

As many times as you like. One definition, many calls: see the three arrows into one machine.

What is the difference between x and 5?

x is the parameter (a name in the definition); 5 is the argument (the value). Move the slider to change the argument.

Where does the returned value go?

Back to the place of the call, so you can use it in the next line. Watch the gold block return.

Why does is_even(7) give False?

Because 7 blocks cannot all be paired: one is left alone and the lamp is red.

How do I check my own answer for maximum(a, b)?

Pick maximum in the free-play step and compare the gold tower with your guess.

What is a subprogram and why use it?

A subprogram is a part of a program with its own name that does one job. Some languages call it a procedure, some a function, some a method. You define it once and call it whenever you need it.

Why use subprograms?

Defining and calling

In Python a subprogram starts with def, then the name, brackets, a colon, and an indented body. To use it, write its name with brackets: this is a call.

def square(x):
    return x * x

print(square(5))   # 25

When the program reaches square(5), it jumps into the subprogram, runs its lines, and then comes back to the same place. A subprogram must be defined before the line that calls it runs.

Parameters and arguments

A subprogram often needs inputs. The names written in the definition are its parameters (like x in def square(x)). The values you give in the call are the arguments (like 5 in square(5)). The first argument goes to the first parameter, the second to the second, and so on, so the order matters.

Variables created inside a subprogram, and its parameters, are local: they live only while the subprogram runs. Other parts of the program cannot see them. So two different subprograms can both use a name like x without a clash.

A subprogram can have many parameters: def area(a, b): return a * b and area(3, 5) gives 15. It can also have none: def hello(): print("Hi").

Procedures and functions: return

A procedure does an action (like printing a line) and gives nothing back. A function works something out and gives a value back with return. The returned value can be stored or used in an expression:

def add(a, b):
    return a + b

total = add(2, 3) + 1   # total is 6

When Python meets return, the subprogram stops at once and sends the value back. A subprogram with no return sends back the special value None. Note: print shows a value on the screen; return hands the value to the program. They are not the same.

Logical functions

A logical (boolean) function returns only True or False. It answers a yes/no question about its arguments. Good names start with is_ or has_.

def is_even(n):
    return n % 2 == 0

if is_even(10):
    print("even")

You can use the call right inside a condition: if is_even(n):. Here are two more:

def in_range(x, a, b):
    return a <= x <= b

def is_leap(y):
    return (y % 4 == 0 and y % 100 != 0) or y % 400 == 0

Logical functions keep the main program short: the hard test is hidden behind a clear name, and you can reuse it, for example to count how many numbers in a list pass the test.

Key formulas and definitions

Worked examples

1. With def square(x): return x * x, what is square(4) + square(3)?

square(4) = 16 and square(3) = 9. 16 + 9 = 25.

2. def area(a, b): return a * b. What does area(3, 5) return?

a = 3 and b = 5, so it returns 3 × 5 = 15.

3. maximum(a, b) returns a if a > b, otherwise b. What is maximum(7, 12)?

7 > 12 is False, so the else part runs and it returns 12.

4. is_even(n) returns n % 2 == 0. Find is_even(9) and is_even(10).

9 % 2 = 1, so 1 == 0 is False. 10 % 2 = 0, so 0 == 0 is True. Answer: False and True.

5. Check is_leap(2100), is_leap(2000) and is_leap(2024) using (y % 4 == 0 and y % 100 != 0) or y % 400 == 0.

2100: divisible by 4 and by 100, not by 400, so False. 2000: divisible by 400, so True. 2024: divisible by 4, not by 100, so True.

6. def f(x): return 2 * x + 1. What is f(f(2))?

f(2) = 2 × 2 + 1 = 5. Then f(5) = 2 × 5 + 1 = 11.

Common mistakes

Practice quiz

1. What is a value given in the call, like 5 in square(5), called?
2. What does a logical function return?
3. Which keyword hands a value back from a function?
4. What is the benefit of a subprogram?
5. def f(x): return x + 2. What is f(5)?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is the difference between a parameter and an argument?

A parameter is the name in the definition, for example x in def square(x). An argument is the actual value you give in the call, for example 5 in square(5).

What is the difference between a procedure and a function?

A procedure does an action and returns nothing. A function returns a value that the program can use. In Python both are written with def; a function has a return line.

What is a logical function used for?

It answers a yes/no question, such as is_even(n) or is_prime(n), and returns True or False. You can use it straight inside an if or a while.

Learn first

Learn next

Related lessons

All Computer Science lessons