
Program Execution and Function Definitions in Python
Understand the flow of program execution in Python starting from the main segment to function definitions and function calling. Learn how Python follows guidelines in executing statements and functions. Explore the concept where statements within function bodies are executed only when the function is called. Discover how Python handles function calls that do not return a value.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Program execution begins with first statement of _main_ segment. Def statements are also read but ignored until called. _main_(add.py) num1=float(input( Enter first number: )) num2= float(input( Enter second number )) Sum=calcsum(num1,num2) Print( Sum of two given number is ,sum)
calcSum(x,y) s=x+y return s Output: X=3.0 Y=7.0 S=10.0
_main_(add.py) num1=float(input( Enter first number: )) num2=float(input( Enter second number )) Sum=calcsum(num1,num2) Print( sum of two given number is ,sum) Data: Num1=3.0 Num2=7.0 Sum=10.0
So we can say that for above program the statements were executed as main.1-> main.2 -> main.3->calcSum.1- >calcSum.2 ->main.3->main.4
Statements are executed from top to bottom.When executing a program,python follow these guidelines: 1.Execution always begin at the first statement of the program. Comment lines (lines beginning with #)are ignored.not executed. All other non blank lines are executed.
The statement inside a function-body are not executed until the function is called. If python notices that is a function definition,(def statements) then Python just executes the function header line to determine that it is proper function header and skips/ignore all lines in the function body
If the called function does not return any value,the return statement has no variable or value or expression,then the control jumps back to the line following the function call statement.
def calcSum(x,y): s=x+y return s num1=float(input( Enter first number: )) num2=float(input( Enter second number )) Sum=calcSum(num1,num2) Print( Sum of two given numbers is ,sum)
Line 1 is ignored because it is a comment Line 2 is executed and determined that it is a function header. So entire function-body(line 3 and line 4 is ignored) Lines 6,7 and 8 executed. Line 8 has a function call,so control jumps to the function header(line 2) and then to the first line of the function body. Line3 function returns after line 4 to line containing function call statement lines to line 9.
Function calling another function is called the caller and the function being called is the called function(or callee). In this _main_ is the caller of calcSum() function.
ARGUMENTS AND PARAMETERS def multiply(a,b): print(a*b) Y=3 multiply(12,y) multiply(y,y) x=5 multiply(y,x)
Output: 36 9 15 Arguments:Python refers to the values being passed as arguments. Parameters: values being received as parameters. The alternative names for argument are actual parameters and actual argument. Alternative names for parameter are formal parameter and formal argument.
Arguments in python can be one of these value types: Literals Variables Expressions def multiply(a,b): print(a*b) Y=3 multiply(3,4) #both literal arguments
P=9 multiply(p,5) #both literal arguments #one literal and #one variable argument #one variable and # one expression argument Multiply(p,p+1)
def multiply(a+1,b): header cannot have expressions.It can have just names or identifiers to hold the incoming values. # Error!! A function