As programs grow, writing everything in one long block becomes unmanageable. Sub-programs let you package code under a name and reuse it. This lesson covers procedures, functions, parameters and why modular code is better.
What is a sub-program?
A sub-program is a named, self-contained block of code that performs one task. It is defined once and called from anywhere. The two main kinds are:
- Procedure: performs actions but does not return a value.
- Function: calculates and returns a value to the caller.
Procedures
PROCEDURE greet(name)
output "Hello, " + name
END PROCEDURE
greet("Sam") // Hello, Sam
greet("Priya") // Hello, Priya
The call greet("Sam") sends the argument “Sam” into the parameter name. The terms are often confused: parameters appear in the definition, arguments are the actual values passed in a call.
Functions
FUNCTION area(width, height)
return width * height
END FUNCTION
x = area(4, 5) // x = 20
output area(2, 3) // 6
A function’s returned value can be stored, printed or used in an expression. When return runs, the function ends immediately.
Parameters and scope
Variables created inside a sub-program are local: they exist only while it runs and are invisible outside. This prevents sub-programs from accidentally changing each other’s variables. Information goes in through parameters and out through return values.
Parameters can be passed by:
- Value: the sub-program gets a copy, so changes do not affect the original.
- Reference: the sub-program works on the original, so changes persist. (Arrays and objects are often passed this way.)
Why use sub-programs?
- Reuse: write once, use many times.
- Readability: meaningful names make the main program easy to follow.
- Decomposition: a large problem splits into smaller tasks.
- Testing: each sub-program can be tested separately.
- Teamwork: different people can write different parts.
- Maintenance: fix a bug in one place.
Recursion (HL preview)
A sub-program can call itself. A recursive function needs a base case to stop and a step that moves towards it:
FUNCTION factorial(n)
if n = 0 then
return 1
else
return n * factorial(n - 1)
end if
END FUNCTION
factorial(3) evaluates 3 x 2 x 1 x 1 = 6. Without a base case it would never end.
Check your understanding
What distinguishes a function from a procedure?
- A function returns a value
- A function has no name
- A procedure always returns a value
- Functions cannot have parameters
Answer: A function returns a value. Procedures do not.
In area(4, 5), what are 4 and 5 called?
- Arguments
- Constants only
- Loops
- Arrays
Answer: Arguments. They are the values passed into the parameters.
What is a local variable?
- A variable that exists only within the sub-program where it is declared
- A variable available everywhere
- A variable that never changes
- A type of array
Answer: A variable that exists only within the sub-program where it is declared. This avoids accidental interference.
What does a recursive function need to avoid running forever?
- A base case
- More parameters
- A loop
- A global variable
Answer: A base case. It stops the recursion.
