On the counter at Mrs. Sri's stall there is a blender. Mrs. Sri puts in avocado, milk and sugar, presses the button, and juice comes out. She does not need to know what is inside the machine. The blender also does not decide whether the juice goes into a glass, into a plastic bag, or gets ice first. The blender only takes the ingredients and hands back the result.
A function works like that blender, but for calculations. The ingredients are the values you send. The machine is the steps written inside the function. The juice is the result of the calculation, which the function hands back to the line that uses it. With a function, a calculation used in many places only has to be written once.
FUNCTION, parameters, RETURN
Mrs. Sri's iced tea costs $4 a glass. This function calculates the price of any number of glasses. The result is below the code:
FUNCTION teaPrice(glasses)
RETURN glasses * 4
END FUNCTION
PRINT teaPrice(2)
PRINT teaPrice(5)
8
20
This is what happens, in order:
FUNCTION teaPrice(glasses)makes a function namedteaPrice.glassesin the brackets is its parameter: a variable that holds the value sent to the function.teaPrice(2)calls the function: it runs the function and sends it the value 2.glassesnow holds 2, and the body runs.RETURNhands the result, 8, back to the place where the function was called, andPRINTprints it.- The second call sends 5, and the result is 20.
A function does not run by itself. The lines between FUNCTION and END FUNCTION only run when the function is called. Functions usually go at the top, before the main program (the lines outside the function).
The result can be used again
A function hands back its result and does not print it. So you are free to use the result: store it in a variable, add it, or compare it.
FUNCTION teaPrice(glasses)
RETURN glasses * 4
END FUNCTION
sari ← teaPrice(2)
dodi ← teaPrice(5)
PRINT "Total:", sari + dodi
IF sari + dodi > 25 THEN
PRINT "Free cracker"
END IF
Total: 28
Free cracker
The prices for Sari (2 glasses) and Dodi (5 glasses) are first stored in sari and dodi, then added and compared with 25. If the price of iced tea goes up, you only change one line inside the function.
Why RETURN, not PRINT
Before you read on, guess: if RETURN glasses * 4 is changed to PRINT glasses * 4, what happens on the line sari ← teaPrice(2)?
The number 8 is printed. But then the program stops on that line with the message "Line 5: teaPrice ended without RETURN, so it has no value." PRINT only shows the number on the screen. The function hands nothing back, so there is no value to store in sari. This is like a blender that pours the juice onto the table: you can see the juice, but you cannot move it into a glass.
So a function that calculates should hand back its result with RETURN. The line that calls the function then decides whether to print the result, store it, or calculate more with it.
Parameters and the inside of a function
A function can have more than one parameter, with commas between them. The values you send go into the parameters in order. In this program, 15 goes into price and 2 goes into count:
FUNCTION subtotal(price, count)
RETURN price * count
END FUNCTION
PRINT subtotal(15, 2)
30
A function has its own room for its variables. The variables set inside a function, its parameters included, exist only while that function runs. They also do not change a variable outside the function that has the same name:
FUNCTION teaPrice(glasses)
price ← glasses * 4
RETURN price
END FUNCTION
price ← 100
PRINT teaPrice(2)
PRINT price
8
100
The function sets its own price to 8, but the price outside the function stays 100.
RETURN also ends the function at once. The lines below it inside the function do not run:
FUNCTION pricier(a, b)
IF a > b THEN
RETURN a
END IF
RETURN b
END FUNCTION
PRINT pricier(8, 15)
PRINT pricier(22, 15)
15
22
In the second call, a holds 22 and b holds 15. a > b is TRUE, so RETURN a runs, and the RETURN b below it never runs. In the first call, a > b is FALSE, so the function reaches RETURN b.
In JavaScript later, FUNCTION becomes function and RETURN becomes return. In Python, FUNCTION becomes def, and RETURN is return too.
Two pseudocode rules work differently in JavaScript and Python:
- A function without
RETURN. In pseudocode, the program stops with an error message. In JavaScript and Python, the program does not stop. The function quietly hands backundefined(JavaScript) orNone(Python), which means "no value". IfteaPriceonly prints its result with noreturn, JavaScript givesNaN(not a number) forsari + dodi, with no message at all. Python stops only at the line that adds them, with the messageTypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'. - A variable from outside the function. In pseudocode, a function can't use a variable that is set outside it. JavaScript and Python can read such a variable. If
unitPrice = 4is written outside the function, andteaPriceusesunitPrice, thenteaPrice(2)gives 8. But in Python, if the function changes an outside variable, for example withtotal = total + price, the program stops with the messageUnboundLocalError: cannot access local variable 'total' where it is not associated with a value.
So the habit from pseudocode is still the safest: send values in through parameters, and hand the result back with return.
⚠️ A function that stays quiet, or can't be seen
- Forgetting
RETURN. A function with an empty body, or with only aPRINTinside, hands nothing back. When its result is used, the program stops with the message "Line 5:teaPriceended without RETURN, so it has no value." and suggests "Add RETURN with the value before line 3." - The wrong number of values.
subtotalneeds two values, sosubtotal(15)stops with the message "Line 5:subtotaltakes 2 values, but got 1." Send as many values as the function has parameters, in the same order. - A variable from outside used inside the function. A function can only use its parameters and the variables set inside that function. If
unitPrice ← 4is written outside andunitPriceis then used inside the function, the program stops with the message "Line 2:unitPricecan't be seen inside the functionteaPrice." Send the value in through a parameter:FUNCTION teaPrice(glasses, unitPrice). - A variable from inside the function used outside. The opposite is also true: a
priceset inside the function does not exist outside it. If the lineprice ← 100is removed from the main program,PRINT pricestops with the message "Line 7:priceonly exists inside the functionteaPrice." Use the result thatRETURNhands back: store it first withprice ← teaPrice(2)above thePRINT priceline. RETURNoutside a function. The program stops with the message "Line 1: RETURN only works inside a FUNCTION." In the main program, usePRINT.
TL;DR: FUNCTION name(parameter) ... END FUNCTION makes a function: a calculation with a name, so you can call it (run it) again and again with different values, like teaPrice(2) and teaPrice(5). A parameter is a variable in the brackets that holds the value you send. With more than one parameter, the values go in by order. RETURN hands the result of the calculation back to the line that called the function, and ends the function at once. That result can be stored, added or compared. A result that is only printed with PRINT cannot be used again. A function can only use its parameters and the variables set inside that function, so send the values it needs through its parameters.
⚡ A value in, a result out
Write:
FUNCTION teaPrice(glasses)
RETURN glasses * 4
END FUNCTION
PRINT teaPrice(2)
PRINT teaPrice(5)
Meaning: FUNCTION teaPrice(glasses) makes a function: a calculation with a name, so it can be used again and again. glasses is a parameter, a variable that holds the value that is sent. teaPrice(2) calls (runs) the function with the value 2: glasses holds 2, then RETURN hands the result, 8, back to the place where the function was called. The body of a function does not run by itself, only when the function is called.
⚡ RETURN, not PRINT
Guess first: in the function teaPrice, RETURN glasses * 4 is changed to PRINT glasses * 4. What happens on the line sari ← teaPrice(2), which stores the function's result in sari?
8 is printed, then the program stops on that line with an error. teaPrice ended without RETURN, so there is no value to store in sari.
Meaning: PRINT only shows the number on the screen, and hands nothing back to the line that called the function. A function that calculates should hand back its result with RETURN. A result that is handed back can be stored, added or compared.
⚡ A function has its own room
Write:
FUNCTION teaPrice(glasses)
price ← glasses * 4
RETURN price
END FUNCTION
price ← 100
PRINT teaPrice(2)
PRINT price
Guess first: does the last line print 8 or 100?
It prints 100. The price inside the function and the price outside the function are two different variables.
Meaning: Variables set inside a function, its parameters included, exist only while that function runs, and they do not change a variable outside with the same name. A function also cannot use variables from outside, so the values it needs are sent through its parameters.