Skip to content

Ilm o Irfan

Technologies

  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Explore Courses

Introduction to Python

  • September 21, 2022
  • Home
  • Free Book

Table of Contents

Chapter 24 - Functions

  • Authors
  • Ms. Samavi Salman
  • Dr. Rao Muhammad Adeel Nawab

Quick Recap

  • Quick Recap – Break and Continue Statements

In previous Chapter, I presented

  • Continue Statement
    • Definition
      • The continue statement causes an immediate jump to the top of a loop. It also sometimes lets you avoid statement nesting
    • Purpose
      • The main purpose of Continue is also a loop control statement
        • it forces to execute the next iteration of the loop
  • Break Statement
    • Definition
      • The break statement causes an immediate exit from a loop. Because the code that follows it in the loop is not executed if the break is reached, you can also sometimes avoid nesting by including a break
    • Purpose
      • To exit a loop immediately without running any remaining code in the loop, regardless of the results of any conditional test, use the break statement
      • The break statement directs the flow of your program; you can use it to control which lines of code are executed and which are not, so the program only executes code that you want it to, when you want it to

Functions in Python

  • Function
  • Definition
    • A Function is defined as a piece of Code (or block of Python Statements) to perform a specific Programming Task
  • Purpose
    • The main purpose of Functions, is to develop high-quality, well–organized and reusable Code (Software), by breaking a large Software into small blocks of Codes (i.e., Functions), such that 
      • Each Function is independent of other Functions and also connected to them
  • Importance 
    • Functions help us to efficiently and quickly develop a large Software by breaking it into Functions to perform various tasks 
  • Applications
    • Functions are mathematical building blocks for
      • Designing machines
      • Predicting natural disasters
      • Curing diseases
      • Understanding world economies and for keeping airplanes in the air
      •  Functions can take input from many variables, but always give the same output, unique to that function
  • Advantages
    • Some of the main advantages of Functions are as follows:
      • Code Reusability
      • Code Testing
      • Code Organization 
      • High-quality Documentation 
      • Parallel Tasking
      • Code Readability and Understandability  
      • Easy Bug Fixing
      • Code Updating / Modification 
      • Code Optimization
      • Code Redundancy 
  • Suitable to Use 
    • For developing any Software, it is useful to break it into a set of Functions and then start Software Development 😊
  • Python Program
  • Definition 
    • A Python Program is a collection of one or more Functions
  • Note 
    • A Python Program must contain one Function i.e.,
      • main() Function
    • Execution of Python Program starts from main() Function
  • Representation of Variable vs Function
  • Variable 
    • number1
    • number2
  • Function 
    • pakistan()
    • __main__
  • Main Components of a Function
  • The three main components of a Function are
    • Function Prototype
    • Function Definition 
    • Function Call

Function Prototype

  • Function Prototype
  • Definition
    • A Function Prototype is a function declaration that specifies the data types of its arguments in the parameter list 
    • Functions can be declared implicitly by their appearance in a call. Arguments to functions undergo the default conversions before the call. The number and type of arguments are not checked.
  • Purpose
    • The Function Prototype are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter and return type of the function. The compiler cross-checks the function signatures before calling it.
  • Importance
    • The Function Prototype serves the following:
      • It tells the return type of the data that the function will return
      • It tells the number of arguments passed to the function
      • It tells the data types of each of the passed arguments
      • Also, it tells the order in which the arguments are passed to the function
      • Therefore essentially, the function prototype specifies the input/output interface to the function i.e. what to give to the function and what to expect from the function
      • Prototype of a function is also called signature of the function
  • Advantages
    • It helps the compiler in determining whether a function is called correctly or not. Each time when a function is called, its calling statement is compared with its prototype. In case of any mismatch, compiler reports an error.
  • Function Prototype vs Function Definition
  • The key difference between the Function Prototype and Function Definition is that the Function Prototype only contains the declaration of the function while the Function Definition contains the actual implementation of the function.
  • Python and Function Prototype
  • Python does not have prototyping because you do not need it. Python looks up globals at runtime; this means that when you use writeHello the object is looked up there and then. The object does not need to exist at compile time but does need to exist at runtime.
  • PYTHON DOES NOT HAVE EXPLICIT FUNCTION PROTOTYPES. HOWEVER, FOR CLARITY I WILL WRITE A FUNCTION PROTOTYPE IN COMMENTS
  • Function Prototype
  • Function Prototype comprises of three parts
    • Return Type
    • Function Name
    • List of Arguments / Parameters
  • Four Variations in Function Prototype
  • No Return Type, No Arguments / Parameters 
  • No Return Type, Arguments / Parameters 
  • Return Type, No Arguments / Parameters 
  • Return Type, Arguments / Parameters
  • Important Note
  • There is no explicit Function Prototype when we write Function in Python
  • However, we should write Function Prototype in Comments to completely and correctly understand the 
    • Input(s) and Output of a Function
  • In Sha Allah, in this Chapter, we will use the concept of Function Prototype for each Function
  • Example - No Return Type, No Arguments / Parameters
				
					void pakistan();
void india();
void saudi_arabia();

				
			
  • Example - No Return Type, Arguments / Parameters
				
					int even(int number);
int print(int number);
int modulus(int number1 , int number2);
				
			
  • Example - Return Type, No Arguments / Parameters
				
					def pakistan():
    return "Pakistan"
def india():
    return "India"
def saudi_arabia():
    return "Saudia Arabia"
				
			
  • Example - Return Type, Arguments / Parameters
				
					def even(number):
    return result
def prime(number):
    return result
def modulus(number1 , number2):
    return result

				
			

Types of Functions

  • Types of Functions
  • There are two types of Functions in Python
    • Built-in Functions 
    • User Defined Functions
  • Built-in Function
  • Definition
    • A Built-in Function is defined as a Function whose functionality is pre-defined in Python Programming Language 
  • Examples of Built-in Functions in Python
    • Some of the popular and widely used Built-in Functions in Python are as follows
    • print()
      • Print a Message on Output Screen
    • input()
      • Take Input from a User
    • sqrt()
      • Calculate Square Root of a Number
    • sum()
      • Calculate Sum of Numbers
    • min()
      • Get a Minimum Value from Multiple Values 
    • max()
      • Get a Maximum Value from Multiple Values 
    • pow()
      • Calculate Power of a Number
    • type()
      • Return Type (Data Type) of an Object
  • User Defined Functions
  • Definition
    • A User Defined Function is defined as a Function that you can define yourself in Python Programming Language 
  • Example 
    • pakistan()
    • india()

Function Definition

  • Function Definition
  • Function Definition is made up of two parts
    • Function Header
    • Body of Function
  • Function Header + Body of Function
  • Function Header 
    • Function Header must exactly match Function Prototype 
  • Body of Function
    • It contains the actual Code

Function Call

  • Function Call
  • Function Call 
    • To execute a Function, we must call it
  • Note
    • A Function Call is a Python Statement
    • main() Function is automatically called when we execute a Python Program 
  • Calling Function vs Called Function
  • Calling Function
    • Calling Function is that Function which calls a Function 
  • Called Function
    • Called Function is that Function which is called by another Function 
  • Example - Calling Function vs Called Function
  • Example 1
    • main() Function calls pakistan() Function 
    • Calling Function 
      • main() Function
    • Called Function 
      • pakistan() Function
  • Example 2
    • india() Function calls pakistan() Function 
    • Calling Function 
      • india() Function
    • Called Function 
      • pakistan() Function
  • Important Note - Calling Function vs Called Function
  • When a Function is called
    • The Program Control is moved from Calling Function to the Called Function
    •  When the Body of Called Function is completely executed then 
    • The Program Control is moved back from Called Function to the Calling Function 
  • Calling a Function – No Return Type and No Arguments / Parameters
  • Use Function Name with ()
  • Note
  • A Python Program will terminate, when 
    • All the Python Statements in the main() Function are executed
  • Example 1 - Function in Python
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Country using Functions
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 18-06-2021	
____End__Date___          = 21-06-2021
'''

'''
Purpose of Program
The main purpose of this Python Program is to demonstrate how Program Control moves from one Function to another, when we have multiple Functions in our Python Program.

This program comprises of four Functions.
Main()
Pakistan()
India()
Saudi_Arabia()
'''

# void pakistan();     # Function Prototype 
# void india();        # Function Prototype 
# void saudi_arabia(); # Function Prototype 

# Function Definition 
def pakistan(): # pakistan() Function Header 
    # Body of pakistan() Function 
    print("I am in Pakistan()")
    
# Function Definition 
def india(): # india() Function Header 
    # Body of india() Function 	
    print("I am in India()")
    
# Function Definition 
def saudia_arabia(): # saudia_arabia() Function Header 
    # Body of saudia_arabia() Function 
    print("I am in Saudia_Arabia()")

if __name__ == "__main__":  # main() Function 
    print("I am in Main()")

				
			
Execution of Code – Dry Run
Python Statement
Processing
Memory
Output Screen

Sart (Program Execution) – Execution of Python Program starts from the main() Function. Therefore, Program Control will be with the main() Function when we will start executing (i.e., Dry Run) our Python Program.

Program Control is with the main() Function

print(“I am in Main()”)

Print Message on the Output Screen

–

I am in Main()

End (Program Execution): Python Program will terminate when all the Python Statements in the main() Function are executed. 

As can be noted, all the Python Statements in the main() Function are executed. Therefore, Python Program will be Terminated 😊

  • Example 2 - Function in Python
				
					''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Country using Functions
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 18-06-2021	
____End__Date___          = 21-06-2021
'''

'''
Purpose of Program
The main purpose of this Python Program is to demonstrate how Program Control moves from one Function to another, when we have multiple Functions in our Python Program.

This program comprises of four Functions.
Main()
Pakistan()
India()
Saudi_Arabia()
'''

# void pakistan();     # Function Prototype 
# void india();        # Function Prototype 
# void saudi_arabia(); # Function Prototype 

# Function Definition
def pakistan(): # pakistan() Function Header 
    # Body of pakistan() Function 
    print("I am in Pakistan()")
  
# Function Definition 
def india(): # india() Function Header 
    # Body of india() Function 
    print("I am in India()")
    
# Function Definition 
def saudia_arabia(): # saudia_arabia() Function Header 
    # Body of saudia_arabia() Function 
    print("I am in Saudia_Arabia()")

if __name__ == "__main__":  # main() Function 
    print("I am in Main()")
    saudia_arabia()
    print("Back in Main()")

				
			
Execution of Code – Dry Run
Python Statement
Processing
Memory
Output Screen

Sart (Program Execution) – Execution of Python Program starts from the main() Function. Therefore, Program Control will be with the main() Function when we will start executing (i.e., Dry Run) our Python Program.

Program Control is with the main() Function

print(“I am in Main()”)

Print Message on the Output Screen.

–

I am in Main()

saudia_arabia()

This Python Statement is a Function Call. Program Control will move from Calling Function (main()) to the Called Function (saudia_arabia()). 

 

I am in Main()

Program Control is with the saudia_arabia() Function

print(“I am in Saudia_Arabia()”)

Print Message on the Output Screen. 

–

I am in Main()

I am in Saudia_Arabia()

Body of saudia_arabia() Function is completely executed. Therefore Program Control will move back from Called Function (saudia_arabia()) to the Calling Function (main())

Program Control is with the main() Function

print(“Back in Main()”)

Print Message on the Output Screen.

–

I am in Main()

I am in Saudia_Arabia()

Back in Main()

End (Program Execution): Python Program will terminate when all the Python Statements in the main() Function are executed. 

As can be noted, all the Python Statements in the main() Function are executed. Therefore, Python Program will be Terminated 😊

  • Example 3 - Function in Python
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Country using Functions
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 18-06-2021	
____End__Date___          = 21-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to demonstrate how Program Control moves from one Function to another, when we have multiple Functions in our Python Program.

This program comprises of four Functions.
Main()
Pakistan()
India()
Saudi_Arabia()
'''

# void pakistan();     # Function Prototype 
# void india();        # Function Prototype 
# void saudi_arabia(); # Function Prototype 

# Function Definition 
def pakistan(): # pakistan() Function Header 
    # Body of pakistan() Function 
    print("I am in Pakistan()")
    
# Function Definition 
def india(): # india() Function Header 
    # Body of india() Function 
    print("I am in India()")
    
# Function Definition 
def saudia_arabia(): # saudia_arabia() Function Header 
    # Body of saudia_arabia() Function 
    print("I am in Saudia_Arabia()")

if __name__ == "__main__":  # main() Function 
    print("I am in Main()")
    saudia_arabia()
    print("Back in Main()")
    pakistan()
    print("Again back in Main()")


				
			
Execution of Code – Dry Run
Python Statement
Processing
Memory
Output Screen

Sart (Program Execution) – Execution of Python Program starts from the main() Function. Therefore, Program Control will be with the main() Function when we will start executing (i.e., Dry Run) our Python Program.

Program Control is with the main() Function

print(“I am in Main()”)

Print Message on the Output Screen.

–

I am in Main()

saudia_arabia()

This Python Statement is a Function Call. Program Control will move from Calling Function (main()) to the Called Function (saudia_arabia()).

 

I am in Main()

Program Control is with the saudia_arabia() Function

print(“I am in Saudia_Arabia()”)

Print Message on the Output Screen.

–

I am in Main()

I am in Saudia_Arabia()

Body of saudia_arabia() Function is completely executed. Therefore Program Control will move back from Called Function (saudia_arabia()) to the Calling Function (main())

Program Control is with the main() Function

print(“Back in Main()”)

Print Message on the Output Screen.

–

I am in Main()

I am in Saudia_Arabia()

Back in Main()

pakistan()

This Python Statement is a Function Call. Program Control will move from Calling Function (main()) to the Called Function (pakistan()).

 

I am in Main()

I am in Saudia_Arabia()

Back in Main()

Program Control is with the pakistan() Function

print(“I am in Pakistan()”)

Print Message on the Output Screen.

–

I am in Main()

I am in Saudia_Arabia()

Back in Main()

I am in Pakistan()

Body of pakistan() Function is completely executed. Therefore Program Control will move back from Called Function (pakistan()) to the Calling Function (main())

Program Control is with the main() Function

print(“Again back in Main()”)

Print Message on the Output Screen.

 

I am in Main()

I am in Saudia_Arabia()

Back in Main()

I am in Pakistan()

Again back in Main()

Program Termination: All the Python Statements in the main() Function are executed. Therefore, Python Program will Terminated 😊

  • Example 4 - Function in Python
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Country using Functions
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 18-06-2021	
____End__Date___          = 21-06-2021
'''

'''
Purpose of Program
The main purpose of this Python Program is to demonstrate how Program Control moves from one Function to another, when we have multiple Functions in our Python Program.

This program comprises of four Functions.
Main()
Pakistan()
India()
Saudi_Arabia()
'''

# void pakistan();     # Function Prototype 
# void india();        # Function Prototype 
# void saudi_arabia(); # Function Prototype 

# Function Definition 
def pakistan(): # pakistan() Function Header 
    # Body of pakistan() Function 
    print("I am in Pakistan()")
    india()  # Function Call
 
# Function Definition 
def india(): # india() Function Header 
    # Body of india() Function 
    print("I am in India()")
    saudia_arabia()  # Function Call
    
# Function Definition 
def saudia_arabia(): # saudia_arabia() Function Header 
    # Body of saudia_arabia() Function 
    print("I am in Saudia_Arabia()")

if __name__ == "__main__":  # main() Function 
    print("I am in Main()")
    pakistan() # Function Call
    print("Back in Main()")


				
			
Execution of Code – Dry Run
Python Statement
Processing
Memory
Output Screen

Sart (Program Execution) – Execution of Python Program starts from the main() Function. Therefore, Program Control will be with the main() Function when we will start executing (i.e., Dry Run) our Python Program.

Program Control is with the main() Function

print(“I am in Main()”)

Print Message on the Output Screen.

–

I am in Main()

pakistan()

This Python Statement is a Function Call.  Program Control will move from Calling Function (main()) to Called Function (pakistan()) i.e. Now Program Control is with the pakistan()

Function

 

I am in Main()

Program Control is with the pakistan() Function

print(“I am in Pakistan()”)

Print Message on the Output Screen.

–

I am in Main()

I am in Pakistan()

india()

This Python Statement is a Function Call.  Program Control will move from Calling Function (pakistan()) to Called Function (india()) i.e. Now Program Control is with the india() Function

–

I am in Main()

I am in Pakistan()

Program Control is with the india() Function

print(“I am in India()”)

Print Message on the Output Screen.

–

I am in Main()

I am in Pakistan()

I am in india()

saudia_arabia()

This Python Statement is a Function Call.  Program Control will move from Calling Function (inida()) to Called Function (saudia_arabia()) i.e. Now Program Control is with the saudia_arabia() Function

–

I am in Main()

I am in Pakistan()

I am in india()

Program Control is with the saudia_arabia() Function

print(“I am in  Saudia_Arabia()”)

Print Message on the Output Screen.

–

I am in Main()

I am in Pakistan()

I am in india()

I am in Saudia_Arabia()

  • Body of saudia_arabia() Function is completely executed. Therefore, Program Control will move back from Called Function (saudia_arabia()) to Calling Function (india()) 

Program Control is with the india() Function

  • Body of india() Function is completely executed. Program Control will move back from Called Function (india()) to Calling Function (pakistan()) i.e., Now 

Program Control is with the pakistan() Function

  • Body of pakistan() Function is completely executed Program Control will move back from Called Function (pakistan()) to Calling Function (main()) i.e., Now Program Control is with the main() Function

Program Control is with the main() Function

print(“Back in Main()”)

Print Message on the Output Screen.

–

I am in Main()

I am in Pakistan()

I am in india()

I am in Saudia Arabia()

Back in Main()

End (Program Execution): Python Program will terminate when all the Python Statements in the main() Function are executed. 

As can be noted, all the Python Statements in the main() Function are executed. Therefore, Python Program will be Terminated 😊

TODO and Your Turn​

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write the Output of following Python Programs
      • NOTE – CHECK THE EFFECT ON OUTPUT i.e.,

 

Program 1

# include <stdio.h>

# void pakistan();        Function Prototype 

# void india();           Function Prototype 

# void saudi_arabia();    Function Prototype 

 

# Function Definition 

def india(): # india() Function Header 

    # Body of india() Function 

    saudi_arabia() # Function Call

    print(“I am in India()”)

 

# Function Definition 

def saudi_arabia(): # saudi_arabia() Function Header

    # Body of saudi_arabia() Function 

    print(“I am in Saudia Arabia()”)    

 

# Function Definition 

def pakistan(): # pakistan() Function Header 

    # Body of pakistan() Function 

    india()  # Function Call

    print(“I am in Pakistan()”)

 

# Function Definition 

if __name__ == “__main__”: # main() Function Header

    # Body of main() Function 

    print(“I am in Main()”)

    pakistan(); # Function Call 

    print(“Back in Main()”)

Program 2

# Function Definition 

# include <stdio.h>

# void pakistan();        Function Prototype 

# void india();           Function Prototype 

# void saudi_arabia();    Function Prototype 

 

# Function Definition 

def india(): # india() Function Header 

    # Body of india() Function 

    saudi_arabia() # Function Call

    print(“I am in India()”)

 

# Function Definition 

def saudi_arabia(): # saudi_arabia() Function Header

    # Body of saudi_arabia() Function 

    print(“I am in Saudia Arabia()”)    

 

# Function Definition 

def pakistan(): # pakistan() Function Header 

    # Body of pakistan() Function 

    print(“I am in Pakistan()”)

    india() # Function Call

    saudi_arabia() # Function Call

 

# Function Definition 

if __name__ == “__main__”: # main() Function Header

    # Body of main() Function 

    print(“I am in Main()”)

    pakistan(); # Function Call 

    print(“Back in Main()”)

Program 3

# Function Definition 

# include <stdio.h>

# void pakistan();        Function Prototype 

# void india();           Function Prototype 

# void saudi_arabia();    Function Prototype 

 

# Function Definition 

def india(): # india() Function Header 

    # Body of india() Function 

    print(“I am in India()”)

    saudi_arabia() # Function Call

 

# Function Definition 

def saudi_arabia(): # saudi_arabia() Function Header

    # Body of saudi_arabia() Function 

    print(“I am in Saudia Arabia()”)    

 

# Function Definition 

def pakistan(): # pakistan() Function Header 

    # Body of pakistan() Function 

    print(“I am in Pakistan()”)

 

# Function Definition 

def madina(): # pakistan() Function Header 

    # Body of pakistan() Function 

    print(“I am in Madina()”)

    pakistan()

    india()

    

# Function Definition 

if __name__ == “__main__”: # main() Function Header

    # Body of main() Function 

    print(“I am in Main()”)

    madina(); # Function Call 

    print(“Back in Main()”)

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
  • Questions
    • Write the Output of any Python Programs
      • NOTE – CHECK THE EFFECT ON OUTPUT

Four Variations in Function Prototype

  • Four Variations in Function Prototype
  • No Return Type, No Arguments / Parameters 
  • No Return Type, Arguments / Parameters 
  • Return Type, No Arguments / Parameters 
  • Return Type, Arguments / Parameters
  • Example - No Return Type, No Arguments / Parameters
  • void pakistan();
  • void india();
  • void saudi_arabia();
  • void square();
  • Example - No Return Type, Arguments / Parameters
  • Example 1
    • def square(int);
  • Analysis 
    • Number of Arguments = 1
    • Argument Data Type = Integer
  • Example 2
    • def add(int, int);
  • Analysis 
    • Number of Arguments = 2
    • First Argument Data Type = integer
    • Second Argument Data Type = integer
  • Example 3
    • def add(float, float, float);
  • Analysis 
    • Number of Arguments = 3
    • First Argument Data Type = Float
    • Second Argument Data Type = Float
    • Third Argument Data Type = Float
  • Example 4
    • def sample_function(char, float, int);
  • Analysis 
    • Number of Arguments = 3
    • First Argument Data Type = Character
    • Second Argument Data Type = Float
    • Third Argument Data Type = Integer
  • Summary - No Return Type, Arguments / Parameters
  • You need to know two things
    • Number of Arguments 
    • Data Type of Arguments 
  • Order of Arguments
  • Order of Arguments is very important 
  • Example 
    • def sample_function1(char, float, int);
    • def sample_function2(float, int, char);
  • In above Example, sample_function1() and sample_function2() are entirely different functions (although Number of Argument is Same) because 
  • Order of Arguments is not the same
  • Example - Return Type, No Arguments / Parameters
  • Example 1
    • int square();
  • Analysis
    • This function will return a value of (Data) Type
      • Integer 
  • Example 2
    • float add();
  • Analysis 
    • This function will return a value of (Data) Type
      • Float 
  • Example 3
    • char grade();
  • Analysis 
    • This function will return a value of (Data) Type
      • Character 
  • Number of Values Returned by a Function
  • Question
    • How many values a Function can Return?
  • Answer 
    • A Function can Return only ONE value
  • Example - Return Type, Arguments / Parameters
  • Example 1
    • int square(int);
  • Analysis 
    • Number of Arguments = 1
    • Argument Data Type = Integer
    • Return Type = Integer
  • Example 2
    • float add(float, float);
  • Analysis 
    • Number of Arguments = 2
    • First Argument Data Type = Float
    • Second Argument Data Type = Float
    • Return Type = Float
  • Example 3
    • char grade(float);
  • Analysis 
    • Number of Arguments = 1
    • Argument Data Type = Float
    • Return Type = Character
  • Four Variations in Function Prototype – Input-Processing-Output
  • No Return Type, No Arguments / Parameters 
  • No Return Type, Arguments / Parameters 
  • Return Type, No Arguments / Parameters 
  • Return Type, Arguments / Parameters
  • square() Function
  • Below I will consider the square() Function
    • Number of Inputs = 1
    • Number of Outputs = 1
  • Example 
    • Input = 5
    • Processing = 5 * 5
    • Output = 25
  • Important Note
  • You cannot do Processing Unless and until you have the
    • Input 
  • No Return Type, No Arguments / Parameters – Input-Processing-Output
Calling Function
Called Function

Call the Function

Input + Processing + Output

  • Function Call

square():

  • No Return Type, Arguments / Parameters – Input-Processing-Output
Calling Function
Called Function

Take Input (from User)

 

Call the Function + Pass Input to Called Function as Arguments

Receive Arguments (Input) PASSED by the Calling Function 

 

Input + Processing + Output

  • Function Call 1 – Passing Constant Value
				
					square(5):
				
			
  • Function Call 2 – Passing Value Stored in Variable
  • number = 5;
  • square(number):
  • Function Call 3 – Passing Value obtained after Calculating an Expression
  • square(2 + 3):
  • Function Call 4 – Passing Value obtained after Calculating an Expression
  • number = 3;
  • square(number + 2):
  • Function Call 5 – Passing Value obtained after Calculating an Expression
  • number1 = 3;
  • number2 = 2;
  • square(number1 + number2):
  • Function Call 1
  • result = square():
  • Return Type, No Arguments / Parameters – Input-Processing-Output
Calling Function
Called Function

Call the Function 

 
 

Input + Processing + Output 

 

Return the Output to the Calling Function 

Receive the Output RETURNED by the Called Function and USE it

 
  • Function Call 2
  • number = 5;
  • result = square(number):
  • Return Type, Arguments / Parameters – Input-Processing-Output
Calling Function
Called Function

Take Input (from User)

 

Call the Function + Pass Input to Called Function as Arguments

Receive Arguments (Input_ PASSED by the Calling Function 

 

Input + Processing + Output

 

Return the Output to the Calling Function 

Receive the Output RETURNED by the Called Function and USE it

 
  • Four Variations in Function Prototype – Input-Processing-Output
  • No Return Type, No Arguments / Parameters 
  • No Return Type, Arguments / Parameters 
  • Return Type, No Arguments / Parameters 
  • Return Type, Arguments / Parameters
  • Real-world Task / Problem
  • Real-world Task / Problem
    • Calculate Square of an Integer Number 
  • Input-Processing-Output
    • Input 
      • An Integer Number (called number)
    • Processing 
      • number * number
    • Output
      • Square of number
  • Example – Input-Processing-Output
    • Input 
      • Number = 5
    • Processing 
      • number * number = 5 * 5
    • Output
      • 25
  • Analysis – Real-world Task / Problem
  • Analysis – Input
    • Number of Inputs = 1
    • Data Type of Input = Integer
  • Analysis – Processing
    • Multiplication of Integer Number
  • Analysis – Output 
    • An Integer Number
  • Inputs and Arguments
  • Question 
    • How many Arguments should be PASSED to a Function?
  • Answer
    • Number of Arguments = Number of Inputs 
  • Data Type of Arguments
  • Question 
    • What should be the Data Type of Arguments PASSED to a Function?
  • Answer
    • Data Type of Arguments = Data Type of Inputs
  • Output and Value RETURNED by a Function
  • Question 
    • What is the RELATIONSHIP between Output and Value RETURNED by a Function?
  • Answer
    • Value RETURNED by a Function = Output
  • Data Type of Value RETURNED by a Function
  • Question 
    • What should be the Data Type of Value RETURNED by a Function?
  • Answer
    • Data Type of Value RETURNED by a Function = Data Type of Output
  • Number of Inputs and Outputs in a Real-world Problem
  • Question 
    • Mostly, how many Inputs and Outputs in a Real-world Problem?
  • Answer
    • Inputs can be
      • SINGLE or MULTIPLE
    • Output is mostly 
      • SINGLE
  • Example 1 - Number of Inputs and Outputs in a Real-world Problem
  • Real-world Task / Problem 
    • Calculate Square of an Integer Number
  • Inputs 
    • ONE (An Integer Number)
  • Output 
    • ONE (An Integer Number)
  • For Instance
    • Input 
      • 5
    • Output 
      • 25
  • Example 2 - Number of Inputs and Outputs in a Real-world Problem
  • Real-world Task / Problem 
    • Calculate Sum of Three Float Numbers 
  • Input 
    • THREE (Integer Number 1, Integer Number 2 and Integer Number 3)
  • Output 
    • ONE (An Integer Number) 
  • For Instance
    • Input 
      • 5
      • 7
      • 10
    • Output 
      • 22
  • Example 3 - Number of Inputs and Outputs in a Real-world Problem
  • Real-world Task / Problem 
    • Take Marks as Input from User and Calculate Grade of Students  
  • Input
    • One (Integer / Float Marks)
  • Output
    • ONE (A Character) 
  • For Instance
    • Input 
      • 90
    • Output 
      • A

Scope of Variable

  • Scope of Variable
  • The scope of the variable is simply lifetime of a variable. It is block of code under which a variable is applicable or alive
  • Types of Scope of Variable
  • There Variables can be declared in two ways:
    • Local Variable (Inside a function or a block)
    • Global Variable (Outside of all functions)
  • In the definition of function parameters which are called formal parameters
  • Local Variables
  • Definition
    • The Variables that are declared inside a function / subroutine are called Local Variables
  • Purposes
    • It can only be used inside the subroutine or code block in which it is declared. 
    • The local variable exists until the block of the function is under execution. After that, it will be destroyed automatically
  • Importance
    • The lifetime of the local variable is within its function only, which means the variable exists till the function executes
    • The reason for the limited scope of local variables is that local variables are stored in the stack, which is dynamic in nature and automatically cleans up the data stored within it
    • But by making the variable static with “static” keyword, we can retain the value of local variable
  • Advantages
    • The use of local variables offers a guarantee that the values of variables will remain intact while the task is running
    • If several tasks change a single variable that is running simultaneously, then the result may be unpredictable. But declaring it as local variable solves this issue as each task will create its own instance of the local variable
    • You can give local variables the same name in different functions because they are only recognized by the function, they are declared in
    • Local variables are deleted as soon as any function is over and release the memory space which it occupies
  • Disadvantages
    • The debugging process of a local variable is quite tricky
    • Common data required to pass repeatedly as data sharing is not possible between modules
    • They have a very limited scope
  • Example 1 – Print two Numbers on Output Screen using Local Variables
				
					'''	
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Two Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display numbers on the Output Screen.
'''

if __name__ == "__main__":  # main() Function
    # Input
    number1 = int(input("Enter first Integer Number: "))
    number2 = int(input("Enter second Integer Number: "))
    print("number1 =", number1, "and number2 =", number2)
				
			
  • Example 2 - Sum of two Numbers
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Sum of Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display sum of numbers on the Output Screen.
'''

if __name__ == "__main__":  # main() Function
    # Input
    # Store values in Local Variables 
    number1 = int(input("Enter first Integer Number: "))
    number2 = int(input("Enter second Integer Number: "))
    
    # Processing + Output
    sum = number1 + number1
    print("Sum of", number1, "+", number2, "=",sum)
    return sum
				
			
  • Example 3 - Sum of two Numbers using Functions
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Sum of Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display sum of numbers on the Output Screen.
'''

# Function Definition
def add():
    # local variable initialization
    number1 = 10
    number2 = 20
    sum = number1 + number2
    
    print("Sum of", number1, "+" ,number2,"=", sum)
    return sum

def sum():
    print("Call add Function to print Sum of Numbers on the Output Screen")
    add()

if __name__ == "__main__":  # main() Function
    print("Call sum Function to print the Sum of Numbers")
    sum()   # Function Call


				
			
  • Global Variables
  • Definition
    • The Variables that are declared outside a function / subroutine are called Global Variables.
  • Purposes
    • It has a global scope means it holds its value throughout the lifetime of the program. Hence, it can be accessed throughout the program by any function defined within the program
  • Importance
    • It can be accessed by any function present in the program
    • Once we declare a global variable, its value can be varied as used with different functions
    • The lifetime of the global variable exists till the program executes
    • These variables are stored in fixed memory locations given by the compiler and do not automatically clean up
    • Global variables are mostly used in programming and useful for cases where all the functions need to access the same data
  • Advantages
    • You can access the global variable from all the functions or modules in a program
    • You only require to declare global variable single time outside the modules
    • It is ideally used for storing “constants” as it helps you keep the consistency
    • A Global variable is useful when multiple functions are accessing the same data
  • Disadvantages
    • Too many variables declared as global, then they remain in the memory till program execution is completed. This can cause of Out of Memory issue
    • Data can be modified by any function. Any statement written in the program can change the value of the global variable. This may give unpredictable results in multi-tasking environments
    • If global variables are discontinued due to code refactoring, you will need to change all the modules where they are called
  • Example 1 – Print two Numbers on Output Screen using Global Variables
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Two Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display numbers on the Output Screen.
'''

# Input
# Global Variables
number1 = int(input("Enter first Integer Number: "))
number2 = int(input("Enter second Integer Number: "))

if __name__ == "__main__":  # main() Function
    print("number1 =", number1, "and number2 =", number2)
				
			
  • Example 1 - Sum of two Numbers
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Sum of Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display sum of numbers on the Output Screen.
'''

# Input
# Store values in Global Variables 
number1 = int(input("Enter first Integer Number: "))
number2 = int(input("Enter second Integer Number: "))

if __name__ == "__main__":  # main() Function  
    # Processing + Output
    sum = number1 + number1
    print("Sum of", number1, "+", number2, "=",sum)
    return sum
				
			
  • Example 2 - Sum of two Numbers using Functions
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Sum of Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display sum of numbers on the Output Screen.
'''

# Global variable initialization
number1 = 10
number2 = 20

# Function Definition
def add():
    sum = number1 + number2
    
    print("Sum of", number1, "+" ,number2,"=", sum)
    return sum

def sum():
    print("Call add Function to print Sum of Numbers on the Output Screen")
    add()

if __name__ == "__main__":  # main() Function
    print("Call sum Function to print the Sum of Numbers")
    sum()   # Function Call
def add():
    # Global variable initialization
    global number1
    number1 = 10
    global number2
    number2 = 20

    sum = number1 + number2
    
    print("Sum of", number1, "+" ,number2,"=", sum)
    return sum

def sum():
    print("Call add Function to print Sum of Numbers on the Output Screen")
    add()

if __name__ == "__main__":  # main() Function
    print("Call sum Function to print the Sum of Numbers")
    sum()   # Function Call
				
			
  • Example 2 - Sum of two Numbers using Functions
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Sum of Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display sum of numbers on the Output Screen.
'''

# Global variable initialization
number1 = 10
number2 = 20

# Function Definition
def add():
    sum = number1 + number2
    
    print("Sum of", number1, "+" ,number2,"=", sum)
    return sum

def sum():
    print("Call add Function to print Sum of Numbers on the Output Screen")
    add()

if __name__ == "__main__":  # main() Function
    print("Call sum Function to print the Sum of Numbers")
    sum()   # Function Call
def add():
    # Global variable initialization
    global number1
    number1 = 10
    global number2
    number2 = 20

    sum = number1 + number2
    
    print("Sum of", number1, "+" ,number2,"=", sum)
    return sum

def sum():
    print("Call add Function to print Sum of Numbers on the Output Screen")
    add()

if __name__ == "__main__":  # main() Function
    print("Call sum Function to print the Sum of Numbers")
    sum()   # Function Call
				
			
  • Example 3 - Multiplication of two Numbers using Functions
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Product of Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display sum of numbers on the Output Screen.
'''

def multiply():
    # Local variable initialization
    number1 = 10
    number2 = 20
    product = number1 * number2
    
    print("Product of", number1, "*" ,number2,"=", product)
    return product

def product():
    print("Call product Function to print Product of Numbers on the Output Screen")
    multiply()
    print("Value of number1 ",number1)
    print("Value of number2 ",number2)

if __name__ == "__main__":  # main() Function
    print("Call product Function to print the Product of Numbers")
    product()   # Function Call


				
			
  • Note:
    • The above program will give error i.e., 
      • error: ‘number1’ undeclared (first use in this function)
      • error: ‘number2’ undeclared (first use in this function)
  • Reason 
    • Because the Variables are declared Locally
    • Local Variables can only be used inside that particular function in which they are declared.
    • Local Variables cannot be used in other Functions
  • Example 4 - Multiplication of two Numbers using Functions
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Product of Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display sum of numbers on the Output Screen.
'''
# Global variable initialization
number1 = 10
number2 = 20

def multiply():
    product = number1 * number2
    
    print("Product of", number1, "*" ,number2,"=", product)
    return product

def product():
    print("Call product Function to print Product of Numbers on the Output Screen")
    multiply()
    print("Value of number1 ",number1)
    print("Value of number2 ",number2)

if __name__ == "__main__":  # main() Function
    print("Call product Function to print the Product of Numbers")
    product()   # Function Call
				
			
  • Reason 
    • Because the Variables are declared Locally
    • Global Variables can be used outside functions in which they are declared.
    • Global Variables can be used in other Functions
  • Formal Parameters
  • Definition
    • Formal parameters, are treated as local variables with-in a function and they take precedence over global variables
  • Global Vs. Local Variables Example – Sum of two Numbers
				
					'''
Program Details
__author_                 = Ms. Samavi Salman
__copyright__             = Copyright (C) 2020 Ms. Samavi Suleman
__license__               = Public Domain
__program__name__         = Display Sum of Integer Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 24-06-2021	
____End__Date___          = 28-06-2021
'''
'''
Purpose of Program
The main purpose of this Python Program is to take two Integer Numbers as input from User and Display sum of numbers on the Output Screen.

# Global variable Initialization
a = 20

# Function to add two Integers
def sum(number1, number2):
    print("value of number1 in sum() = ",  number1)
    print("value of number2 in sum() = ",  number2)
    return number1 + number2

if __name__ == "__main__":  # main() Function
    # Local Variable Initialization
    number1 = 10
    number2 = 20
    number3 = 0
    print("value of number1 in main() = ",  number1);
    number3 = sum(number1, number2);
    print("value of number3 in main() = ",  number3);
				
			
  • Difference between Local and Global Variable
Local Variable
Global Variable

It is declared inside a function.

It is declared outside the function.

If it is not initialized, a garbage value is stored

If it is not initialized zero is stored as default.

It is created when the function starts execution and lost when the functions terminate.

It is created before the program’s global execution starts and lost when the program terminates.

Data sharing is not possible as data of the local variable can be accessed by only one function.

Data sharing is possible as multiple functions can access the same global variable.

Parameters passing is required for local variables to access the value in other function

Parameters passing is not necessary for a global variable as it is visible throughout the program

When the value of the local variable is modified in one function, the changes are not visible in another function.

When the value of the global variable is modified in one function changes are visible in the rest of the program.

Local variables can be accessed with the help of statements, inside a function in which they are declared.

You can access global variables by any statement in the program.

It is stored on the stack unless specified.

It is stored on a fixed location decided by the compiler.

TODO and Your Turn

Todo Tasks
Your Turn Tasks
Todo Tasks

TODO Task 1

  • Task 
    • Consider the following Tasks and answer the questions given below
  • Note 
    • Your answer should be 
      • Well Justified
  • Questions
    • Write Python Programs for all the Tasks given below by following the Variation given in this Chapter i.e.,
      • Local Variable
      • Global Variables

 

Task  1

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called subtract()) with declare Local Variables as Input from User and print the Display on the Output Screen.

 

Task  2

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called multiplication()) with declare Global Variables as Input from User and print the Display on the Output Screen.

 

Task  3

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called divide()) with declare Local Variables as Input from User and print the Display on the Output Screen.

 

Task  4

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called modulus()) with declare Global Variables as Input from User and print the Display on the Output Screen.

 

Your Turn Tasks

Your Turn Task 1

  • Task
    • Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
  • Questions
    • Write Python Programs for all the Tasks given below by following the Variation given in this Chapter i.e.,
      • Local Variable
      • Global Variables

Pass Object by Value and Pass Object by Reference

  • Pass Object by Value and Pass Object by Reference
  • An immutable object’s value cannot be changed, even if it is passed a new value
  • Passing by value refers to passing a copy of the value
  • Passing by reference refers to passing the real reference of the variable in memory
  • Example 1 – Pass Value by Object Reference
				
					def square(number):
    number *= number

number = 4
square(number)
print(number)
				
			
  • Example 2 – Pass Value by Object Reference
				
					def square(number):
    return number * number 

square(4)
				
			
  • Example 3 – Pass Value by Object Reference
				
					def square(number):
    # Accept an argument, return a value
    return number * number

result = 4
# Later, reassign the return value:
result = square(result)
print(result)
				
			
  • Example 4 – Pass Value by Object Reference
				
					def square(text,number):
    # Return multiple values
    return f"Square of Number, {number}!", number

# Later, reassign each return value by unpacking.
text, number = greet("4", 4)
print(text)
				
			

Chapter Summary

  • Chapter Summary

In this Chapter, I presented the following main concepts:

  • Function
    • A Function is defined as a piece of Code (or block of Python Statements) to perform a specific Programming Task
  • Python Program 
    • A Python Program is a collection of one or more Functions
  • Main Components of a Function 
    • The three main components of a Function are
      • Function Prototype
      • Function Definition 
      • Function Call
  • Function Prototype
    • A Function Prototype is a function declaration that specifies the data types of its arguments in the parameter list 
    • Functions can be declared implicitly by their appearance in a call. Arguments to functions undergo the default conversions before the call. The number and type of arguments are not checked
  • Function Prototype
    • Function Prototype comprises of three parts
      • Return Type
      • Function Name
      • List of Arguments / Parameters
  • Four Variations in Function Prototype
    • No Return Type, No Arguments / Parameters 
    • No Return Type, Arguments / Parameters 
    • Return Type, No Arguments / Parameters 
    • Return Type, Arguments / Parameters 
  • Types of Functions 
    • There are two types of Functions in Python
      • Built-in Functions 
      • User Defined Functions 
  • Built-in Function
    • A Built-in Function is defined as a Function whose functionality is pre-defined in Python Programming Language 
  • User Defined Functions
    • A User Defined Function is defined as a Function that you can define yourself in Python Programming Language 
  • Function Definition 
    • Function Definition is made up of two parts
      • Function Header
      • Body of Function 
  • Scope of Variable
    • The scope of the variable is simply lifetime of a variable. It is block of code under which a variable is applicable or alive
  • Types of Scope of Variable
    • There Variables can be declared in two ways:
      • Local Variable (Inside a function or a block)
      • Global Variable (Outside of all functions)
    • In the definition of function parameters which are called formal parameters
  • Local Variables
    • The Variables that are declared inside a function / subroutine are called Local Variables
  • Global Variables
    • The Variables that are declared outside a function / subroutine are called Global Variables
  • Formal Parameters
    • Formal parameters, are treated as local variables with-in a function and they take precedence over global variables
  • Pass Object by Value and Pass Object by Reference
    • An immutable object’s value cannot be changed, even if it is passed a new value
  • Passing by value refers to passing a copy of the value
    • Passing by reference refers to passing the real reference of the variable in memory

In Next Chapter

  • In Next Chapter
  • In Sha Allah, in the next Chapter, I will present a detailed discussion on
  • Data Structure in Python
Chapter 23 - Break and Continue Statements
  • Previous
Chapter 28 – Basics of Data Structures
  • Next
Share this article!
Facebook
Twitter
LinkedIn
About Us

Ilm O Irfan Technologies believes that honest dedicated hard work with the ethical approach of commitment true to every word, and trust-building is the road to success in any field of business or life.

Quick Links
  • About Us
  • Contact
Useful Links
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • Support
  • FAQ
Subscribe Our Newsletter
Facebook Twitter Linkedin Instagram Youtube

© 2022 Ilmo Irfan. All Rights Reserved.