8.2. Parameters and Arguments or the difference between a function definition and a function call

As we have already said, functions are named sequences of statements that execute a specific task. A function definition is the construction that gives a name to this special sequence of statements. Whereas a function call is the execution of this sequence of statements. We have also seen that the execution can be parameterized. This means, that there are special variables, the parameters, in the function which are bound to values, the arguments, during the function call.

Flow of execution.  Some precision about the flow of execution. If you call a function the statements of the function body are executed. The execution of the current sequence of statements is interrupted and the flow of execution jumps to the sequence of statements named by the function. The function statements are executed and then the flow of execution continues with the sequence from where the function was called.

Caution

The statements in the function body are only executed at a function call, so all errors in this piece of code are only reported at the execution.

Now if we remember that within a function you can call other functions, the above scheme becomes rapidly disturbing, but Python keeps a track from where a function is called and there is only one statement at a time that is executed. You can visualize this using a stack diagram of the function calls.

Stack

A stack is an ordered set of things which can be accessed in two ways. You can put things on the top of the stack, and only take back the thing on the top of the stack. A common example is a stack of plates (it is hard to get a plate from within the stack). A stack is sometimes called a LIFO an abbreviation of last-in-first-out.

So what Python does is to handle a stack of function calls. If a function is called, Python puts the position from where it is called on the stack, executes the function and when the function execution is finished Python gets the position to continue back from the stack. Figure 8.3 shows the stack diagram of the execution of the gc function (Example 8.1).

Figure 8.3. Stack diagram of function calls