Results
After this: Hand a value back with return and use it where the call was written.
On this page · 7 sections · 4 checks
You ask a friend to measure a room and work out its floor area. There are two things the friend can do with the answer: shout it across the house, or come back and hand you a note with the number on it. If you want to add the areas of two rooms, you need the notes.
So far, a function could only shout: it printed. This lesson gives a function a way to hand a value back.
Handing a value back
The word return inside a function means: stop here, and hand this value back to the line that called me. The value lands exactly where the call was written, as if the call had been replaced by the value.
Work through print(area(3, 4)):
- The first line names the function
area, with the parameterswandh. - The program reaches the print line. Before it can print, it needs the value of
area(3, 4), so it calls the function. A frame appears withwholding 3 andhholding 4. - The function's line works out
w * h, which is 12, and returns it. The frame is gone. - Back on the calling line,
area(3, 4)now stands for 12, and print shows12.
The narration says it in one sentence: "area returns 12 to line 47".
Watch it happen
- Run the programRun it. Watch the frame for area appear with w and h in it, and watch the narration when the function returns: the 12 travels back to the line that called it.
def area(w, h): return w * h
print(area(3, 4))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Using the result
A call that returns a value can be used anywhere a value can: put it in a box, print it, add it to something. Here two rooms are measured, and the two results are added.
def area(w, h): return w * h
room = area(3, 4)hall = area(2, 5)print(room + hall)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
room becomes 12 and hall becomes 10. Each call opened its own frame, handed back its result and closed. Then the program added the two boxes: 22.
One more thing return does: it ends the function at once. Lines after the return that ran do not run. The last check of this lesson uses that.
The code
def area(w, h): return w * h
print(area(3, 4))12
public class Main { static int area(int w, int h) { return w * h; }
public static void main(String[] args) { System.out.println(area(3, 4)); }}12
print is not return
The two are mixed up by almost everyone at first, because both seem to "give" something. They do different jobs.
printshows a value on the screen. The calling line receives nothing.returnhands a value to the calling line. Nothing is shown unless that line prints it.
In the trace below, shout prints and loud returns.
def shout(word): print(word + "!")
def loud(word): return word + "!"
print(shout("hi"))print(loud("hi"))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Run it
Add a second print line with area(10, 2), and run it again.
Check yourself
area is the function from this lesson: it returns w * h. What does the call area(5, 2) hand back?
shout is the function from the trace above: it prints word + "!" and has no return line. A program writes word = shout("yo")String word = shout("yo");. What does the box word hold afterwards?
Complete the missing line so that area hands back w times h, and the program prints 12.
def area(w, h): ____________________ # ← hand back w times h
print(area(3, 4))public class Main { static int area(int w, int h) { ____________________ // ← hand back w times h }
public static void main(String[] args) { System.out.println(area(3, 4)); }}return ends the function at once. What does this print?
def bigger(a, b): if a > b: return a return b
print(bigger(4, 9))public class Main { static int bigger(int a, int b) { if (a > b) { return a; } return b; }
public static void main(String[] args) { System.out.println(bigger(4, 9)); }}- Hand a value back with return, and say that the value lands where the call was written.
- Put a returned value in a box, print it, or use it in arithmetic.
- Say that return ends the function at once, and that a function without return hands back nothing (None in Python; void in Java).
- Tell print from return: print shows a value on the screen, return gives it to the calling line.