The Call Stack

Function calls are stacked one on top of another. When a program calls a function it stacks that function on top of the program. If that function calls another function it stacks that function on top of the function that called it. The program keeps track of the call stack and when it reaches the return statement of any function it returns to where it was in the previous function or program.

Example:

            Pam         
        Bob Bob Bob     Tim  
    Al  Al  Al  Al  Al  Al  Al 
--- --- --- --- --- --- --- --- ---

When programs call a function Python creates a frame object on top of the call stack. The frame object stores the line number of the original function call. When a function returns python removes a frame object from the top of the stack and returns to the line number where the previous function or program left off.