In this post , we will see how to write flow of program execution in python. We will see examples to better understand this concepts.
Flow of Execution in a function call
In order to understand what flow of program execution is, in terms of programming language, read the following lines carefully.
The flow of execution refers to the order in which statements are executed during program run.
When you are working with functions it is really important to know the order in which statements are executed. This is called the flow of execution.
Let us discuss the actual flow of execution
✔️Execution always begins at the first statement of the program. Statements are executed one at a time, in order, from top to bottom.
✔️ Comment lines are ignored and not executed. All other non blank lines are executed.
✔️ Function definitions do not alter the flow of execution of the program, but remember that statements inside the function are not executed until the function is called.
✔️ Function calls plays important role in the flow of execution. Instead of going to the next statement, the flow jumps to the first line of the called function, executes all the statements there, and then comes back to pick up where it left off.
✔️ Each time a function completes, the program picks up where it left off in the function that called it. When it gets to the end of the program, it terminates.
✔️ A function can define another function inside it. That is called nested function. Function can be inner function and outer function. The function which is defined inside the other function is called inner function and the function which contain inner function is called outer function. Notice that, the inner function definition is not executed until the outer function is called.
This is all about flow of execution in python. Let us now see how all this is done with the help of an example. Consider the following code
Example code 1 :
def power(a,b): y = a ** b return y def calcSquare(s): c = power(s,2) return c n = 10 result = calcSquare(n) + power(2,2) print(result)
Solution : Flow of execution for above code will be:
✔️ Python execution begins with first statement, it notices that it is a function definition and skips over all of the lines in the function definition until it finds a line that it no longer included in the function (line 5). Here def statements are also read but ignored until called.
It then notices line 5 is also a function definition. Python just execute the function header line to determine that it is proper function header and again skips all lines in the function body to line 9. On line 10 it notices it has two functions to execute, so it goes back and executes first function that is calcSquare(n) and return calculated result of same function . After that control jump to the second function that is power(2,2) on line 10. It goes back to power function and execute the function body and return calculated result. . Control jump back to the function call statement on line 10 and completes it arithmetic operation.It also completes the assignment in line 10. Finally, it will go to line 11 and print the result.
Output produced by above code is 104
Example code 2 :
def power(a,b): y = a ** b return y def calcSquare(s): c = power(s,2) return c n = 5 result = calcSquare(n) print(result)
Flow of execution for above code is 1 –>5—>9—>10—>5—>6—>1—>2—>3—>6—>7—>10—>11
Line 2 is executed and determine it is a function header, so entire function body is( lines 2,3) ignored. Line 5 is executed and determine it is a function header, so netire function body ( lines 6,7) is ignored. Line 9 is executed; Line 10 has a function call, so control jumps to the function header (line 5) but there is function inside function . Inner function is executed; control jump to line 1 and then function body lines 2,3 are executed. Function return after line 3 to line containing function call statement ( line 7) Lines 6,7 are executed. Control jump back to line 10 containing function call statement and then line 11.
Output produced by above code is 25
Example code 3 :
def increment(x): x = x + 1 # main program x = 3 print(x) increment(x) print(x)
Flow of execution for above code is 1 –>5—>6—>7—>1—>2—>8
First, control fall on line 1 and determine it is function definition so it skips function body (line 2 ) . Line 4 is ignored because it is comment line . It is not executed. Control jump to line 5 and it is executed. Line 6 is executed. Control fall on line 7 , it contain function call, so control jump to line 1 and execute the function body( line 2 ) . Control jump back to line 8 and it is finally executed . Notice that, if the called function does not return any value,then control jump back to the line following the function call statement.
Output produced by above code is
- Data Structure Quiz
- DBMS Quiz
- MySQL Queries
- Python Assignments
- Python Example
- Python projects
- Python Quizz