Head office, Rana Pratap Bagh, Delhi011 2721 1512+91 97116 99511

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.

Get the whole book: Computers and coding for classes 9 and 10 (6 lessons, free to print)

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.

Calling a function and getting a value backOn the left, a main program box lists a = 5 and b = square(a). An arrow labelled argument 5 goes to a function box on the right that contains FUNCTION square(n) and RETURN n * n. A second arrow labelled returns 25 goes back to the main program, where b = 25 is written. Main program a = 5 b = square(a) OUTPUT b b becomes 25 FUNCTION square(n) n is the parameter here n = 5 RETURN n * n 5 * 5 = 25 argument 5 returns 25 Define once, call many times The function has its own variable n; it is local to the function.
The argument travels into the function, the body runs, and the return value travels back to where the call was made.

Worked examples

See how it works

  1. 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.

  2. 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.

  3. 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.

  4. 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

  1. On one card write FUNCTION addFive(n): RETURN n + 5. On another write FUNCTION triple(n): RETURN n * 3. These are your function definitions.
  2. A friend writes a main program on paper: a = 2, b = addFive(a), c = triple(b), OUTPUT c.
  3. 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.
  4. Continue: triple(7) returns 21, so c = 21. Say the output aloud.
  5. 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

Write your answers in a notebook, or print this sheet and write on it. When you finish, mark your work with the answer key.

Practice sheet: Functions: reusing steps (Classes 9-10, Computers and coding)

  1. What are the four parts of a function named in the lesson?
  2. What is the difference between a parameter and an argument?
  3. FUNCTION cube(n): RETURN n * n * n. What does cube(3) return?
  4. FUNCTION area(l, b): RETURN l * b. What does area(9, 6) return?
  5. Does defining a function run its steps? When do they run?
  6. Trace: a = 4, b = double(a), c = double(b) + 1, where double(x) returns x * 2. What is c?
  7. What does local scope mean?
  8. Give two reasons for using functions.
  9. FUNCTION circleArea(r): RETURN 3.14 * square(r). What is circleArea(2)?
  10. Write pseudocode for a function that returns the average of three numbers.

Answer key

Answers: Functions: reusing steps

Do the practice sheet first, then check here. If an answer is different from yours, read the lesson again and try once more.

  1. Name, parameters, body and return value.
  2. A parameter is the name used inside the function definition (n); an argument is the actual value passed in a call (7).
  3. 27
  4. 54
  5. No; the steps run only when the function is called.
  6. 17: b = 8, double(8) = 16, plus 1 is 17.
  7. Variables made inside a function belong to that function only and cannot be seen outside it.
  8. Any two of: reuse the steps, fix a mistake in one place, make the program easier to read.
  9. 12.56, because square(2) = 4 and 3.14 * 4 = 12.56.
  10. FUNCTION average(a, b, c): RETURN (a + b + c) / 3.
WhatsApp us