Easy flow of function execution,parameters,arguments-2

Flow of function execution,uses,parameters & arguments

Flow of function execution

Flow of execution is order in which statements are executed in a script.
It is important to define a function before using the function.
Execution always begins from the first line of the program. Statements are executed one at a time, in order from top to bottom.
The interpreter finds main function and starts executing from there in sequence.
Statements inside function will be executed only if function is called.
Function definition do not alter or change flow of execution.
The interpreter keeps a track and after completion of function the program resumes from where it left in the function that is called.

Uses of functions

Various reasons for using functions are:
It makes code easy to read and debug.
It allows us to name group of statements using that name we can call the function.
It makes code modules and increases modularity of code.
When required to update we can just change the module in one place.
It reduces chances of errors.
We can re-use the function in script.
Avoids repetition of code.

Parameters and Arguments

Whenever we define a function, its header consist of parentheses which may or may not contain parameter.
Parameter is defined by giving a name that appear in head of function or function definition and arguments are given while calling the function.
Parameter is present in method signature are called as formal parameters or formal argument and arguments passed in function call to invoke the defined function are called as actual parameters or actual argument.

Example of formal parameter and actual parameter

""" Author : ITVoyagers (itvoyagers.in)
Date :31st August 2020
Description : Program for showing formal parameters and actual parameters
"""
Formal parameters and actual parameter
<strong>Formal parameter and actual parameter</strong>

Variables and Parameters are Local

Local variable is a variable present inside the function definition.
Parameters can be accessible only inside function and has no application outside function body.

Pass-by-value parameter passing

The value of the actual parameter is copied to the formal parameter. With pass-by-value, there is no way for the actual parameter to change value as the function gets its own copy.

In Python, scalar values are sent by-value. Lists and other objects are sent by reference.

def add_me(a):
     return a+5
 
# main
b=5
op = add_me(b)
print ("Output is ", op)
print ("Output of b is ", b)

Output

>>> 
Output is  10
Output of b is  5

Pass-by-reference parameter passing

The reference to the actual parameter is sent to the function. When we execute a script, and a list is sent, we don’t copy the list to the actual parameter, we draw an arrow from formal to actual.

def update_me(list1):      
     list1[1]=99
     print("list1 is", list1) 
# main
list2 = [5,2,9]
update_me(list2)

Output

>>> 
>>>
list1 is [5, 99, 9]
>>> 

Note: Names of formal and actual parameters can be same or diffrenet.

For other python basics related posts:

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon.......



For other advanced python related posts:

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in 're' module(Part I)- Match vs Search
Functions in 're' module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon.......

Leave a Comment