Article
Return values
Ask a friend, "What is 4 doubled?" They can shout the answer so everyone hears it. Or they can hand you the answer so you can use it in your next sum. A procedure can do both. Showing an answer is DISPLAY. Handing it back is RETURN.
RETURN gives a value back
A procedure can finish by sending a value back to the line that called it. That value is the return value. In AP pseudocode you write RETURN(expression). In Python you write return expression.
PROCEDURE double(n)
{
RETURN(n * 2)
}
answer ← double(4)
DISPLAY(answer)The call double(4) is replaced by the value it returns, which is 8. So answer becomes 8, and the program displays 8. A call that returns a value can be used anywhere a value can be used:
The output is 8, 9, 8, and done. The inner call double(2) is worked out first and gives 4. Then double(4) gives 8.
Look at the line double(4) on its own. It runs, and it returns 8, but nothing is displayed and nothing is stored. The value is thrown away. A returned value is lost unless you store it or use it.
Check your understanding
What is displayed as a result of running this program?
PROCEDURE double(n)
{
RETURN(n * 2)
}
double(5)
DISPLAY("Done")RETURN ends the procedure at once
When the computer reaches a RETURN, the procedure stops immediately. Any lines after it are never run.
PROCEDURE test(n)
{
RETURN(n + 1)
DISPLAY("never")
}
DISPLAY(test(5))The program displays 6 only. The word never is not displayed, because the procedure ended at the RETURN.
This is useful with an IF. A procedure can return early and skip the rest:
PROCEDURE sign(n)
{
IF(n < 0)
{
RETURN("negative")
}
RETURN("not negative")
}
DISPLAY(sign(-3))
DISPLAY(sign(5))For -3, the IF is true, so the procedure returns "negative" and stops. For 5, the IF is false, so the computer skips the block and reaches the second RETURN. The output is negative and not negative.
A procedure that returns true or false is very common. You do not need an IF to build one. A comparison already gives true or false, as you saw in the logical equivalence lesson:
PROCEDURE isEven(n)
{
RETURN(n MOD 2 = 0)
}
DISPLAY(isEven(6))
DISPLAY(isEven(7))The program displays true and then false. In Python the same code displays True and False.
Check your understanding
What is displayed as a result of running this program?
PROCEDURE test(n)
{
RETURN(n + 1)
DISPLAY("after")
}
DISPLAY(test(1))DISPLAY or RETURN?
DISPLAY | RETURN | |
|---|---|---|
| What it does | Shows a value on the screen | Sends a value back to the caller |
| Does the procedure stop? | No | Yes, at once |
| Can the program use the value? | No | Yes, if you store it or use it |
A procedure with DISPLAY and no RETURN gives nothing back. In Python such a procedure gives back a special value called None:
The first line of output is 8, from the print inside the procedure. The second is None, because showDouble returned nothing. Use RETURN when another part of the program needs the answer.
Worked example
PROCEDURE bill(price, count)
{
RETURN(price * count)
}
total ← bill(3, 4)
DISPLAY(total + 5)- The call
bill(3, 4)setspriceto 3 andcountto 4. - The procedure works out
3 * 4, which is 12, and returns it. - The call is replaced by 12, so
totalbecomes 12. DISPLAY(total + 5)shows 12 + 5, which is17.
Common mistakes
- Expecting RETURN to show the value. RETURN only hands it back. Use DISPLAY on the result to show it.
- Writing lines after a RETURN and expecting them to run. They never do.
- Calling a procedure and ignoring what it returns.
double(4)alone does nothing visible. Writex ← double(4)orDISPLAY(double(4)). - Forgetting to RETURN on some path. If an IF returns and the other path has no RETURN, Python gives
None. - Using
DISPLAYwhen you need the value later. A displayed value cannot be used by the program.
On the exam
You will be given a procedure with a RETURN and asked what a call gives, or what is displayed after it is stored. Work from the inside out: evaluate the arguments, run the procedure to the first RETURN, put the returned value where the call was, then continue. Watch for code after a RETURN, and for calls whose value is never used.
Key words
Return value: the value a procedure sends back to the line that called it.
RETURN: the statement that ends a procedure and sends back a value.
None: Python's value for "nothing", given back by a procedure that has no RETURN.