- Computers and coding
- Classes 9-10
- 5 min read
Functions: reusing steps
A function is a named block of steps you define once and call many times, with parameters going in and a return value coming out.
Let us begin
The big idea
If you need the same five steps in three places, you could write them three times. Programmers do better: they write the steps once, give the block a name, and call it whenever they need it. That named block is a function.
A function has a name, a list of parameters (the values it needs), a body (the steps) and often a return value (the answer it gives back). In pseudocode: FUNCTION square(n): RETURN n * n. The name is square, the parameter is n, and the body returns n times n.
Defining a function does nothing by itself. The steps run only when the function is called, for example square(7). The value 7 is called the argument; inside the function, n becomes 7, the body runs, and 49 is returned to the place where it was called.
Functions can take more than one parameter, such as FUNCTION area(length, breadth): RETURN length * breadth. Each call can pass different arguments: area(3, 4) returns 12 and area(10, 2) returns 20. Variables made inside a function belong to that function only; this is called local scope.
Why bother? Three reasons. Reuse: write once, call many times. Fewer mistakes: fix a bug in one place and every call is fixed. Readability: a line like total = average(marks) says what it does without showing how.
Worked examples
See how it works
Tracing a call
FUNCTION double(x): RETURN x * 2. Main program: a = 5, b = double(a), c = double(b). Trace: a = 5. Call double(5): x is 5, returns 10, so b = 10. Call double(10): x is 10, returns 20, so c = 20.
A function with two parameters
FUNCTION perimeter(l, b): RETURN 2 * (l + b). Call perimeter(7, 3): l is 7, b is 3, 7 + 3 = 10, 2 * 10 = 20. The function returns 20.
A function that calls another
FUNCTION circleArea(r): RETURN 3.14 * square(r). Call circleArea(10): square(10) returns 100, then 3.14 * 100 = 314. Reusing square keeps circleArea short.
Why reuse matters
A shop program works out a bill with tax in six places. If the tax rate changes and the sum is copied six times, you must change six lines and might miss one. With one function withTax(price), you change one line.
Try it at home
Function cards
You will need
- Index cards or paper cut into cards
- A pencil
- A bowl of small coins or beans for values
What to do
- On one card write FUNCTION addFive(n): RETURN n + 5. On another write FUNCTION triple(n): RETURN n * 3. These are your function definitions.
- A friend writes a main program on paper: a = 2, b = addFive(a), c = triple(b), OUTPUT c.
- Act it out. When the program calls addFive(2), hand the addFive card 2 beans; the card holder adds 5 beans and hands back 7. Write b = 7.
- Continue: triple(7) returns 21, so c = 21. Say the output aloud.
- Write a new function card of your own, such as half(n), and run a program that uses it twice.
Practice sheet
Check what you learned
Practice sheet: Functions: reusing steps (Classes 9-10, Computers and coding)
- What are the four parts of a function named in the lesson?
- What is the difference between a parameter and an argument?
- FUNCTION cube(n): RETURN n * n * n. What does cube(3) return?
- FUNCTION area(l, b): RETURN l * b. What does area(9, 6) return?
- Does defining a function run its steps? When do they run?
- Trace: a = 4, b = double(a), c = double(b) + 1, where double(x) returns x * 2. What is c?
- What does local scope mean?
- Give two reasons for using functions.
- FUNCTION circleArea(r): RETURN 3.14 * square(r). What is circleArea(2)?
- Write pseudocode for a function that returns the average of three numbers.
Answer key
Answers: Functions: reusing steps
- Name, parameters, body and return value.
- A parameter is the name used inside the function definition (n); an argument is the actual value passed in a call (7).
- 27
- 54
- No; the steps run only when the function is called.
- 17: b = 8, double(8) = 16, plus 1 is 17.
- Variables made inside a function belong to that function only and cannot be seen outside it.
- Any two of: reuse the steps, fix a mistake in one place, make the program easier to read.
- 12.56, because square(2) = 4 and 3.14 * 4 = 12.56.
- FUNCTION average(a, b, c): RETURN (a + b + c) / 3.
