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?
- No repeating: the same lines are written once.
- Easy to fix: change one place and every call is fixed.
- Easy to read: a good name like
is_primetells what happens. - Easy to share work: a big task is cut into small tasks, and each is a subprogram.
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)) # 25When 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 6When 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 == 0Logical 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
- def name(parameters): body → call: name(arguments)
- Parameter = name in the definition; argument = value in the call; order matters
- Function: return value; procedure: no value (Python gives None)
- Logical function returns True or False: if is_even(n): ...
- Local variables and parameters live only inside the subprogram
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
- Writing print instead of return inside a function and then trying to use the result. print only shows it; the call gives back None.
- Mixing the order of arguments. For def power(a, n), power(2, 3) is not the same as power(3, 2).
- Using a local variable outside its subprogram. A variable made inside a function is gone after the call.
- Forgetting the brackets: square is the subprogram; square(5) is the call and gives a value.