Functions calling functions
After this: Follow a call inside a call, and say which frames are open at any moment.
On this page · 7 sections · 3 checks
A recipe says "make the sauce". The sauce has its own recipe, and that one says "chop the onions". While you chop, three jobs are open at once: the dish, the sauce and the chopping. You finish the chopping and go back to the sauce; you finish the sauce and go back to the dish.
A function can call another function in the same way. This lesson is about what the trace shows when it does.
A call inside a call
Start with one small function, doubletwice, which returns its input times two. Then write quad, which doubles twice: it returns double(double(x))twice(twice(x)).
Work through quad(3):
- The program calls
quad(3). A frame forquadappears, withxholding 3. - To return double(double(3))twice(twice(3)),
quadfirst needs the value of the inner call, so it calls double(3)twice(3). A second frame appears, andquadis now waiting. - The inner call returns 6, and its frame is gone. Back in
quad, the inner call now stands for 6. - Only now does the outer call start: double(6)twice(6). A frame appears again, returns 12, and is gone.
quadreturns 12 to the program, and print shows12.
The two calls to doubletwice never overlap. The inner one finishes before the outer one begins. So at any moment at most two function frames are open: quad and one doubletwice.
def double(x): return x * 2
def quad(x): return double(double(x))
print(quad(3))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
Three deep
Now add oct, which returns double(quad(x))twice(quad(x)). With oct(3):
- The program calls
oct(3): one function frame. octneedsquad(3)first, so it calls it: two frames, withoctwaiting.quadneeds double(3)twice(3), so it calls it: three frames. This is the deepest moment. Three functions are open at once,oct,quadand doubletwice. Only the innermost is running; the other two are waiting.- The frames close from the inside out: 6 goes back to
quad, the second doubling gives 12, andquadreturns 12 tooct. octdoubles that. 24 goes back to the program, and print shows24.
The band counts the frames for you. The program itself has a frame too, so the counter says 4 frames at the deepest moment.
- Run the programRun it and watch the Variables panel count the frames. Find the step where three functions are open besides the program, and read which one is running and which are waiting.
def double(x): return x * 2
def quad(x): return double(double(x))
def oct(x): return double(quad(x))
print(oct(3))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
The rule
A call inside a call works like any other call: the inner function gets a new frame, and the outer one waits. The newest frame is always the one running. When a function returns, its frame is removed and its value goes to the frame below it, which carries on from where it stopped. Frames close in the opposite order from the one they opened in.
The code
def double(x): return x * 2
def quad(x): return double(double(x))
def oct(x): return double(quad(x))
print(oct(3))24
public class Main { static int twice(int x) { return x * 2; }
static int quad(int x) { return twice(twice(x)); }
static int oct(int x) { return twice(quad(x)); }
public static void main(String[] args) { System.out.println(oct(3)); }}24
Run it
Change the 3 to a 5, and work out the answer before you press Run.
Check yourself
In the run of oct(3), the first call to doubletwice is running. Which functions are open at that moment, besides the program itself?
quad returns double(double(x))twice(twice(x)). With x holding 3, what happens?
Work it out from the inside out. What does this print?
def up(x): return x + 1
def climb(x): return up(up(x)) * 2
print(climb(5))public class Main { static int up(int x) { return x + 1; }
static int climb(int x) { return up(up(x)) * 2; }
public static void main(String[] args) { System.out.println(climb(5)); }}Honesty notes
- Follow a call inside a call: the inner call finishes first, and its result becomes the argument of the outer one.
- Count the frames open at any moment of a trace, and say which one is running and which are waiting.
- Say why a frame disappears when its function returns, and where the returned value goes.
- Work out what a chain of small functions gives for a number, by hand.