Flow of execution in a function call:
To understand the program execution we have to know how a program is organized in the
computer's primary memory(RAM). Below is a diagram given of a over simplified memory layout diagram
This diagram will be used in our further discussion
As we know that any function in order to work, has to be called with proper arguments.
So we are going to look into how a function works, starting from the function call till result generation.
We will consider a very simple addition program that adds two numbers.
def ad(a,b):
return a+b
print(ad(10,30))
The above code takes 5 steps to be executed. Underneath we have listed the steps
Points to remember:
1. Execution of a program starts from the first line of the code, executes one line at a time from top to bottom.2.The function definition never changes the sequence of execution of a program but the statements inside the function
are only executed when the function is called.
3. A function can be called from inside of another function.
4. A function can be declared inside of another function declaration.
5. This idea is similar to recursion.Don’t read the program from top to bottom but read the execution flow
References:
2. Pythontutor
3. Computer Science with python Sumita arora