Lesson 04-08: Functions within Functions

Learning Target: I can write functions that call other functions.
Learning Target: I can explain how the call stack works.

You can call functions from within functions! Check it out:

Now if we look at the codelens for the same code:


You’ll notice a few things:

  • Steps 5 through 12 start from f1() all the way into f3(), printing out the strings.
  • During this time, we see f1, f2, f3, all stacked on top of each other.
  • Steps 13 through 15 show each function returning None as each of f1/f2/f3 disappears.

The stack of function names you see is called the call stack.

The Call Stack

Every time you call a function, that function is added to the call stack. The call stack keeps track of where the program will return to when a function ends.

In the above example, since f2() is called inside of f1(), f1() can’t be complete until f2() completes. Same with f2() and f3().

A call stack can only behave in two ways, either by:
  • adding a new function onto the top of the stack (when a function is called)
  • removing the current function from the top of the stack (when a function completes and returns a value)

True to its name, a call stack just “stacks” functions on top of each other, in the order that it was called. We always know which function we will be returning to, since it is the next function in the stack.

Next Section - Lesson 04-09: Program Design Tips: Part II