Visualizing Function Calls in Python – Step by Step

visualizing function callsThe following are notes I made to break down, step by step, the video “Visualizing Function Calls” from the Coursera course “Learn to Program: The Fundamentals“, by Paul Gries and Jennifer Campbell.

In the video, Paul uses the Python Visualizer to demonstrate how Python executes a program, line by line. There’s a lot going on during this class, and a lot of important concepts that are shown quite fast; so in order to understand it myself I created some comprehensive notes for me to understand every step. I also thought it could be useful to others, so I’m sharing it on this blog, and at the same time I’m inaugurating with this article. Hope it brings some value.

Visualizing Function Calls

Python visualizer:

Python Visualizing Function Calls

The first thing the visualizer does is read the whole Function Definition

Python Visualizing Function Calls 2

A variable called convert_to_minutes is created, and it stores a memory address (x1) of a function object ( convert_to_minutes(num_hours) ). This function object contains all the information about the function:

  • The parameter
  • The docstring
  • Any code that needs to be executed

At this point we’re paused on line 10, which contains an assignment statement. The variable minutes_2 has not yet been created, because it will happen AFTER we execute the assignment statement.


Rules for executing an Assignment Statement

  1. Evaluate the expression at the right hand side, producing a memory address
  2. The memory address is stored in the variable

When the assignmen statement is executed, the first thing that will happen is that the Function Call at the right hand side ( convert_to_minutes(2) ) will be evaluated. In order to execute this FUNCTION CALL the ARGUMENT (2) needs to be evaluated. This will create a new region of memory to keep track of what happens WHILE convert_to_minutes is executed.

So, when we go forward the new region of memory (stack frame) for the convert_to_minutes is created:

 Python Visualizing Function Calls

A Stack Frame for the function call conver_to_minutes(2) is created, and the variable num_hours (which is the parameter of the convert_to_minutes function) will store the memory address (x2) for the value 2 (that was passed with the function call)

Then, we move forward to the body of the function in line 7:

 Python Visualizing Function Calls

Line 7 contains an assignment statement. Python evaluates the right hand side. We look the value of num_hours, and it’s 2.

2 times 60 is 120, so we’re going to see a value 120 get created, and we’re going to see a variable result get created INSIDE the function convert_to_minutes:

 Python Visualizing Function Calls

So now the Stack Frame of convert_to_minutes contains now 2 variables:

num_hours: which stores memory address x2, which stores value 2

result: which stores memory address x3, which stores value 120

On the next step Python is going to execute the Return Statement:


Rules for executing a Return Statement:

  1. Evaluates the expression
  2. Produces a memory address
  3. Passes back the memory address to the caller

According to the rules of execution of the Return Statement the expression (at the right hand side) is going to be evaluated first (in this case: result) which POINTS TO THE VALUE 120, and it’s going to yield it’s memory address (x3) and store it in the variable return:

 Python Visualizing Function Calls

When Python moves forward, it’s going to exit the function convert_to_minutes. And control is going to pass back to where Python was before calling the function: to the Main Frame, where the variable minutes_2 will appear storing the memory address x3, which stores the value 120. The stack frame for the call to convert_to_minutes disappears from computer memory, that’s what happens when a function exits. At his point python is going to be positioned on line 11:

 Python Visualizing Function Calls

, ,

Leave your comment

Loading Disqus Comments ...
Loading Facebook Comments ...