Skip to main content

Unit 3 · Topic 3.12

3.12 Calling Procedures

A procedure is a named chunk of code you can run whenever you need it, by calling its name. This topic covers parameters and arguments, how a call pauses the program to run the procedure, and how RETURN sends a value back.

Key terms

  • procedure
  • parameter
  • argument
  • procedure call
  • return value

Procedures, parameters and arguments

A procedure is a named group of instructions that can take inputs and can return a value. Other languages call them functions or methods.

Parameters are the input variables listed in the procedure's definition. Arguments are the actual values you pass in when you call it. In the call addTax(40, 0.25) below, 40 and 0.25 are arguments; they're assigned to the parameters price and rate, in order.

The reference sheet shows two forms: a procedure that just runs statements, and one that ends with RETURN(expression) to send a value back. To call a procedure, write its name with arguments: procName(arg1, arg2). To keep a returned value, assign it: result ← procName(arg1, arg2).

What happens during a call

A call interrupts the normal top-to-bottom order. The program jumps into the procedure, runs its statements, and when it reaches the end or a RETURN, it jumps back to the point right after the call, carrying any returned value with it.

PROCEDURE addTax(price, rate) { tax ← price * rate RETURN(price + tax) } DISPLAY("Total:") total ← addTax(40, 0.25) DISPLAY(total)

This displays "Total:" first, then calls addTax(40, 0.25). Inside, price is 40 and rate is 0.25, so tax = 40 * 0.25 = 10, and it returns 40 + 10 = 50. Back in the main code, total = 50, which is displayed. The output is Total: 50.

RETURN ends the procedure immediately

RETURN can appear anywhere in a procedure, and as soon as it runs, the procedure stops, even in the middle of a loop. Anything after it in the procedure is skipped for that call.

Procedures that don't return a value are useful for their effects, like displaying something or changing a list. Procedures that return a value can be used inside expressions, like addTax(40, 0.25) + 5.

An argument can be any expression, not just a plain value. In addTax(price * 2, rate), the expression price * 2 is worked out first, and its value is what gets assigned to the parameter. A procedure can also have no parameters at all, like ROTATE_LEFT(), which still needs its parentheses when called.

If a procedure returns a value but the call doesn't store or use it, as in a line that's just addTax(40, 0.25), the returned value is simply lost.

Built-in procedures

The reference sheet includes procedures you call the same way:

  • DISPLAY(expression) shows the value followed by a space.
  • INPUT() gets a value from the user and returns it, so name ← INPUT() stores what the user typed.
  • RANDOM(a, b), LENGTH(aList), APPEND, INSERT and REMOVE, plus the robot commands.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Tracing calls inside an expression

    What does this display?PROCEDURE double(n) { DISPLAY("in") RETURN(n * 2) } x ← 3 DISPLAY("start") y ← double(x) + double(1) DISPLAY(y)

    Show the solution
    1. Step 1: x = 3, then "start" is displayed.
    2. Step 2: To work out double(x) + double(1), the program calls double(3) first: it displays "in" and returns 6.
    3. Step 3: Then it calls double(1): it displays "in" again and returns 2.
    4. Step 4: y = 6 + 2 = 8, which is displayed.

    Answer: start in in 8

  2. Example 2

    RETURN inside a loop

    What does this display?PROCEDURE firstNegative(nums) { FOR EACH n IN nums { IF (n < 0) { RETURN(n) } } RETURN(0) } DISPLAY(firstNegative([4, -1, 7, -6])) DISPLAY(firstNegative([2, 5]))

    Show the solution
    1. Step 1: First call with [4, -1, 7, -6]: 4 isn't negative. -1 is, so RETURN(-1) runs and the procedure stops at once. The -6 is never reached.
    2. Step 2: Second call with [2, 5]: no element is negative, so the loop ends normally and RETURN(0) runs.

    Answer: -1 0

Common mistakes

  • Mixing up parameters (the names in the definition) and arguments (the values in the call).
  • Assuming the code after a RETURN still runs. RETURN ends the procedure immediately.
  • Forgetting that a procedure's DISPLAY output appears at the moment it's called, which may be in the middle of another line's work.

On the exam

  • Expect to trace programs with one or more procedure calls, sometimes calls inside expressions or loops. Track where the program jumps and what each call returns.
  • Written Response 2(b) can ask about calls to your own procedure, such as what a specific call returns or which arguments would make it behave differently. Know your procedure's parameters and what it returns.

Connected topics

Videos

Check yourself

4 questions on 3.12 Calling Procedures. Pick an answer to see if you got it, and why.

Question 1 of 4

What is displayed when the following program is run? PROCEDURE mystery(a, b) { IF (a > b) { RETURN (a - b) } RETURN (b - a) } x ← mystery(3, 10) y ← mystery(x, 2) DISPLAY(x + y)

Question 2 of 4

What is displayed when the following program is run? PROCEDURE check(n) { IF (n MOD 2 = 0) { RETURN ("even") } DISPLAY("odd") RETURN ("not even") } DISPLAY(check(4)) DISPLAY(check(7))

Question 3 of 4

A program contains the procedure PROCEDURE rectArea(length, width), which returns length * width, and the statement area ← rectArea(5, 8). Which of the following are the arguments in this call?

Question 4 of 4

What is displayed when the following program is run? PROCEDURE addTwice(n, k) { n ← n + k n ← n + k RETURN (n) } a ← 5 b ← addTwice(a, 3) DISPLAY(a) DISPLAY(b)

0 of 4 answered