Chapter 26  A Step by Step Example to Write Functions II
 Authors
 Ms. Samavi Salman
 Dr. Rao Muhammad Adeel Nawab
 Supporting Material
Solving Programming Tasks using Functions
 Steps – Solving Programming Tasks using Functions
 In Sha Allah, we will follow the following Steps to solve the Programming Tasks using Functions
 Step 1: Completely and Correctly Understand the Realworld Task
 Step 1.1: Realworld Task
 Step 1.2: Realworld Task Description
 Step 1.3: Your Job – As a Software Developer
 Step 2: Can we treat the Realworld Task (Step 1.2) as a Programming Task?
 Step 2.1: Check Whether the Realworld Task can be treated as a Programming Task?
 Step 2.1.1: Write Down Input and Output of the Realworld Task
 Step 2.1.1.1: Example 1 – Input and Output of the Realworld Task
 Step 2.1.1.2: Example 2 – Input and Output of the Realworld Task
 Step 2.1.1.3: Example 3 – Input and Output of the Realworld Task
 Step 2.1.2: Conclusion
 Step 2.1.1: Write Down Input and Output of the Realworld Task
 Step 2.2: Convert the Realworld Task Description (Step 1.2) into Programming Task Description
 Step 2.1: Check Whether the Realworld Task can be treated as a Programming Task?
 Step 3: Completely and Correctly Understand the Programming Task Description
 Step 3.1: Completely and Correctly Understand the Programming Task Description
 Step 3.2: Requirements Gathering
 Step 3.3: Requirements Analysis
 Step 4: Plan and Design Solution to the Programming Task
 Step 4.1: Identify InputProcessingOutput (from Steps 3.1, 3.2, 3.3)
 Step 4.2: Write down Function Prototype
 Step 4.3: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Programming Task
 Step 4.4: Convert Algorithm (Step 4.3) into Pseudo Code
 Step 4.5: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 4.4)
 Step 5: Implementation
 Step 6: Dry Run the Python Program
 Step 7: Analysis, Main Findings and Conclusion
 Step 7.1: Analysis of Python Program
 Step 7.2: Summary of Analysis
 Step 7.3: Main Findings
 Step 7.4: Conclusion
 Step 1: Completely and Correctly Understand the Realworld Task
Example 1  Solving Programming Tasks using Functions (with Function Prototype: No Return Type, No Arguments / Parameters)
 Example  Steps (Solving Programming Tasks using Functions)
 Realworld Task
 Calculate Grade of Students
 In Sha Allah, in the next Slides we will solve the Realworld Task (i.e., Calculate Grade of Students) using Functions by following the Steps mentioned above
 Note
 The Function Prototype will be
 No Return Type, No Arguments / Parameters
 The Function Prototype will be
 Example 1  Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the Realworld Task 
Step 1.1: Realworld Task 

Step 1.2: Realworld Task Description 

Step 1.3: Your Job – As a Software Developer 

Step 2: Can we treat the Realworld Task (Step 1.2) as a Programming Task? 
Step 2.1: Check Whether the Realworld Task can be treated as a Programming Task? 

Step 2.1.1: Write Down Input and Output of the Realworld Task 

Step 2.1.1.1: Example 1 – Input and Output of the Realworld Task 

Step 2.1.1.2: Example 2 – Input and Output of the Realworld Task 

Step 2.1.1.3: Example 3 – Input and Output of the Realworld Task 

Step 2.1.2: Conclusion 

Step 2.2: Convert the Realworld Task Description (Step 1.2) into Programming Task Description 

Step 3: Completely and Correctly Understand the Programming Task Description 
Step 3.1: Completely and Correctly Understand the Programming Task Description 

Step 3.2: Requirements Gathering 

Step 3.3: Requirements Analysis 

Step 4: Plan and Design Solution to the Programming Task 
Step 4.1: Identify InputProcessingOutput (from Steps 3.1, 3.2, 3.3) 

Step 4.2: Write down Function Prototype 

Step 4.3: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Programming Task 

Step 4.4: Convert Algorithm (Step 4.3) into Pseudo Code 
Function main() FUNCTION CALL – grade_calculation1() End Function Function grade_calculation1() INPUT – marks IF marks >= 80 and marks <= 100 grade – A ELIF marks >= 70 and marks < 80 grade – B ELIF marks >= 60 and marks < 70 grade – C ELIF marks >= 50 and marks < 60 grade – D ELIF marks >= 0 and marks < 50 grade – F
DISPLAY – marks, grade End Function 
Step 4.5: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 4.4) 
Step 5: Implementation 
'''
Program Details
__author_ = Ms. Samavi Salman
__copyright__ = Copyright (C) 2021 Ms. Samavi Suleman
__license__ = Public Domain
__program__name__ = Calculate Grade of a Student in Math
__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__ = 18062021
____End__Date___ = 21062021
'''
'''
Purpose of Program
The main purpose of this Python Program is to demonstrate how we can use Functions to calculate grade of a student in Math and display it on the Output Screen. Please note that the Function Prototype will be: No Return Type, No Arguments / Parameters 😊
This program comprises of two Functions.
1. main()
2. grade_calculation1()
'''
# Function Prototypes for User Defined Functions
# void grade_calculation1(); # Function Prototype
def grade_calculation1():
try:
# Input
# Variable to store Input  marks
# Variable to store Output  grade
marks = float(input("Enter your marks in Math: "))
# Processing + Output
if marks >= 80 and marks <= 100:
grade = 'A'
elif marks >= 70 and marks < 80:
grade = 'B'
elif marks >= 60 and marks < 70:
grade = 'C'
elif marks >= 50 and marks < 60:
grade = 'D'
elif marks >= 0 and marks < 50:
grade = 'F'
# Display Input and Output
print("Your Marks in Math are: ",marks)
print("You Grade in Math is: ", grade)
except ValueError:
print("Error! Enter Integer/Float Value")
if __name__ == "__main__": # main() Function
grade_calculation1() # Function Call
Enter your marks in Math: 70.5
Your Marks in Math are: 70.5
You Grade in Math is: B
Step 6: Dry Run the Python Program  
Python Statement  Processing  Memory  Output Screen 
Start (Program Execution) – Execution of Python Program starts from the main() Function. Therefore, Program Control will be with the main() Function 😊  
Program Control is with the main() Function  
grade_calculation1()  This Python Statement is a Function Call. Therefore, Program Control will move from Calling Function (main()) to the Called Function ( grade_calculation1()).  –  – 
Program Control is with the grade_calculation1() Function  
marks = float(input(“Enter your marks in Math: “))  Print Message on the Output Screen and take Marks as Input from User  marks = 70.5 (In grade_calculation1() Function)  Enter your marks in Math: 70.5 
The next Python Statement is a nested Selection Structure. There are two Possible Outcomes of a Condition: (1) True or (2) False.
 
IF marks >= 80 and marks <= 100:  Check whether the Condition is True or False
· 70.5 >= 80 and 70.5 <= 100 · False and False · False The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘A’) will be skipped and Program Control will move to the next Condition in the nested Selection Structure  marks = 70.5 (In grade_calculation1() Function)  Enter your marks in Math: 70.5 
grade – ‘A’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘A’) will be skipped  marks = 70.5 (In grade_calculation1() Function)  
ELIF marks >= 70 and marks < 80:  Check whether the Condition is True or False
· 70.5 >= 70 and 70.5 < 80 · True and True · True The Condition is True, Python Statements immediately after the Condition will be executed and Program Control will exit from the nested Selection Structure  marks = 70.5 (In grade_calculation1() Function)  Enter your marks in Math: 70.5 
grade – ‘B’  Store B in Variable grade (and exit from the nested Selection Structure)  marks = 70.5 (In grade_calculation1() Function) grade = B (In grade_calculation1() Function)  Enter your marks in Math: 70.5 
ELIF marks >= 60 and marks < 70:  Check whether the Condition is True or False
· 70.5 >= 60 and 70.5 <= 70 · False and False · False The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘C’) will be skipped and Program Control will move to the next Condition in the nested Selection Structure  marks = 70.5 (In grade_calculation1() Function) grade = B (In grade_calculation1() Function)  Enter your marks in Math: 70.5 
grade – ‘C’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘C’) will be skipped  marks = 70.5 (In grade_calculation1() Function) grade = B (In grade_calculation1() Function)  
ELIF marks >= 50 and marks < 60:  Check whether the Condition is True or False
· 70.5 >= 50 and 70.5 < 60 · False and False · False The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘D’) will be skipped and Program Control will move to the next Condition in the nested Selection Structure  marks = 70.5 (In grade_calculation1() Function) grade = B (In grade_calculation1() Function)  Enter your marks in Math: 70.5 
grade – ‘D’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘D’) will be skipped  marks = 70.5 (In grade_calculation1() Function) grade = B (In grade_calculation1() Function)  Enter your marks in Math: 70.5 
ELIF marks >= 0 and marks < 50:  Check whether the Condition is True or False
· 70.5 >= 0 and 70.5 < 50 · False and False · False The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘F’) will be skipped and Program Control will move to the next Condition in the nested Selection Structure  marks = 70.5 (In grade_calculation1() Function) grade = B (In grade_calculation1() Function)  Enter your marks in Math: 70.5 
grade – ‘F’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘F’) will be skipped  marks = 70.5 (In grade_calculation1() Function) grade = B (In grade_calculation1() Function)  Enter your marks in Math: 70.5 
print(“Your Marks in Math are: “,marks)  Print Marks (Input) on the Output Screen.  marks = 70.5 (In grade_calculation1() Function) grade = B (In grade_calculation1() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 
print(“You Grade in Math is: “, grade)  Print Grade (Output) on the Output Screen.  marks = 70.5 (In grade_calculation1() Function) grade = B (In grade_calculation1() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
Body of grade_calculation1() Function is completely executed. Variables declared in the grade_calculation1() Function will be destroyed and Program Control will move back from the Called Function ( grade_calculation1()) to the Calling Function (main())  
Program Control is with the main() Function  
–  –  –  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
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. Variables declared in the main() Function will be destroyed and Python Program will be Terminated 😊 
Step 7: Analysis, Main Findings and Conclusion  
Step 7.1: Analysis of Python Program – Calling a Function (No Return Type, No Arguments / Parameters)  
 
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters)  
 
Step 7.3: Main Findings  
 
Step 7.4: Conclusion  

Example 2  Solving Programming Tasks using Functions (with Function Prototype: No Return Type, Arguments / Parameters)
 Example  Steps (Solving Programming Tasks using Functions)
 Realworld Task
 Calculate Grade of Students
 In Sha Allah, in the next s we will solve the Realworld Task (i.e., Calculate Grade of Students) using Functions by following the Steps mentioned above
 Note
 The Function Prototype will be
 No Return Type, Arguments / Parameters
 The Function Prototype will be
 Example 2  Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the Realworld Task 
Step 1.1: Realworld Task 

Step 1.2: Realworld Task Description 

Step 1.3: Your Job – As a Software Developer 

Step 2: Can we treat the Realworld Task (Step 1.2) as a Programming Task? 
Step 2.1: Check Whether the Realworld Task can be treated as a Programming Task? 

Step 2.1.1: Write Down Input and Output of the Realworld Task 

Step 2.1.1.1: Example 1 – Input and Output of the Realworld Task 

Step 2.1.1.2: Example 2 – Input and Output of the Realworld Task 

Step 2.1.1.3: Example 3 – Input and Output of the Realworld Task 

Step 2.1.2: Conclusion 

Step 2.2: Convert the Realworld Task Description (Step 1.2) into Programming Task Description 

Step 3: Completely and Correctly Understand the Programming Task Description 
Step 3.1: Completely and Correctly Understand the Programming Task Description 

Step 3.2: Requirements Gathering 

Step 3.3: Requirements Analysis 

Step 4: Plan and Design Solution to the Programming Task 
Step 4.1: Identify InputProcessingOutput (from Steps 3.1, 3.2, 3.3) 

Step 4.2: Write down Function Prototype 

Step 4.3: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Programming Task 

Step 4.4: Convert Algorithm (Step 4.3) into Pseudo Code 
Function main() INPUT – marks FUNCTION CALL – grade_calculation2() End Function Function grade_calculation2() IF marks >= 80 and marks <= 100 grade – A ELIF marks >= 70 and marks < 80 grade – B ELIF marks >= 60 and marks < 70 grade – C ELIF marks >= 50 and marks < 60 grade – D ELIF marks >= 0 and marks < 50 grade – F
DISPLAY – marks, grade End Function 
Step 4.5: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 4.4) 
Step 5: Implementation 
'''
Program Details
__author_ = Ms. Samavi Salman
__copyright__ = Copyright (C) 2021 Ms. Samavi Suleman
__license__ = Public Domain
__program__name__ = Calculate Grade of a Student in Math
__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__ = 18062021
____End__Date___ = 21062021
'''
'''
Purpose of Program
The main purpose of this Python Program is to demonstrate how we can use Functions to calculate grade of a student in Math and display it on the Output Screen. Please note that the Function Prototype will be: No Return Type, Arguments / Parameters 😊
This program comprises of two Functions.
3. main()
4. grade_calculation2()
'''
# Function Prototypes for User Defined Functions
# void grade_calculation2(marks); # Function Prototype
def grade_calculation2(marks):
# Processing + Output
if marks >= 80 and marks <= 100:
grade = 'A'
elif marks >= 70 and marks < 80:
grade = 'B'
elif marks >= 60 and marks < 70:
grade = 'C'
elif marks >= 50 and marks < 60:
grade = 'D'
elif marks >= 0 and marks < 50:
grade = 'F'
# Display Input and Output
print("Your Marks in Math are: ",marks)
print("You Grade in Math is: ", grade)
if __name__ == "__main__": # main() Function
try:
# Input
# Variable to store Input  marks
# Variable to store Output  grade
marks = float(input("Enter your marks in Math: "))
grade_calculation2(marks) # Function Call
except ValueError:
print("Error! Enter Integer/Float Value")
Enter your marks in Math: 70.5
Your Marks in Math are: 70.5
You Grade in Math is: B
Step 6: Dry Run the Python Program 

Python Statement 
Processing 
Memory 
Output Screen 
Start (Program Execution) – Execution of Python Program starts from the main() Function. Therefore, Program Control will be with the main() Function 😊  
Program Control is with the main() Function  
marks = float(input(“Enter your marks in Math: “))  Print Message on the Output Screen and take Marks as Input from User  marks = 70.5 (In main() Function)  Enter your marks in Math: 70.5 
grade_calculation2(marks)  This Python Statement is a Function Call. Therefore, Program Control will move from Calling Function (main()) to the Called Function ( grade_calculation2()).  marks = 70.5 (In main() Function)  Enter your marks in Math: 70.5 
Program Control has moved from Calling Function (i.e., main() Function) to the Called Function (i.e., grade_calculation2(marks) Function). Variable marks (i.e., 70.5) has been PASSED as an Argument / Parameter to the Called Function (i.e., grade_calculation2(marks))  
Program Control is with the grade_calculation2() Function  
Function Header – grade_calculation2()  


–  –  marks = 70.5 (In main() Function)  Enter your marks in Math: 70.5 
The next Python Statement is a nested Selection Structure. There are two Possible Outcomes of a Condition: (1) True or (2) False.


IF marks >= 80 and marks <= 100:  Check whether the Condition is True or False

marks = 70.5 (In main() Function)  Enter your marks in Math: 70.5 
grade – ‘A’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘A’) will be skipped  
ELIF marks >= 70 and marks < 80:  Check whether the Condition is True or False

marks = 70.5 (In main() Function)  Enter your marks in Math: 70.5 
grade – ‘B’  Store B in Variable grade (and exit from the nested Selection Structure)  marks = 70.5 (In main() Function) grade = B (In grade_calculation2() Function)  Enter your marks in Math: 70.5 
ELIF marks >= 60 and marks < 70:  Check whether the Condition is True or False

marks = 70.5 (In main() Function) grade = B (In grade_calculation2() Function)  Enter your marks in Math: 70.5 
grade – ‘C’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘C’) will be skipped  marks = 70.5 (In main() Function) grade = B (In grade_calculation2() Function)  
ELIF marks >= 50 and marks < 60:  Check whether the Condition is True or False

marks = 70.5 (In main() Function) grade = B (In grade_calculation2() Function)  Enter your marks in Math: 70.5 
grade – ‘D’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘D’) will be skipped  marks = 70.5 (In main() Function) grade = B (In grade_calculation2() Function)  Enter your marks in Math: 70.5 
ELIF marks >= 0 and marks < 50:  Check whether the Condition is True or False

marks = 70.5 (In main() Function) grade = B (In grade_calculation2() Function)  Enter your marks in Math: 70.5 
grade – ‘F’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘F’) will be skipped  marks = 70.5 (In main() Function) grade = B (In grade_calculation2() Function)  Enter your marks in Math: 70.5 
print(“Your Marks in Math are: “,marks)  Print Marks (Input) on the Output Screen.  marks = 70.5 (In main() Function) grade = B (In grade_calculation2() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 
print(“You Grade in Math is: “, grade)  Print Grade (Output) on the Output Screen.  marks = 70.5 (In main() Function) grade = B (In grade_calculation2() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
Body of grade_calculation2() Function is completely executed. Variables declared in the grade_calculation2() Function will be destroyed and Program Control will move back from the Called Function ( grade_calculation2()) to the Calling Function (main())  
Program Control is with the main() Function  
–  –  marks = 70.5 (In main() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
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. Variables declared in the main() Function will be destroyed and Python Program will be Terminated 😊 
Step 7: Analysis, Main Findings and Conclusion 

Step 7.1: Analysis of Python Program – Calling a Function (No Return Type, No Arguments / Parameters) 



Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters) 



Step 7.3: Main Findings 



Step 7.4: Conclusion 


 Comparing Two Variations of Prototype
Analysis Parameters  No Return Type, No Arguments / Parameters  No Return Type, Arguments / Parameters 
Function Prototype  void grade_calculation01();  void grade_calculation02(float marks) 
Function Call  grade_calculation01();  grade_calculation02(marks) 
Function Header  def grade_calculation01()  def grade_calculation02(marks) 
Body of Function  Contains Code for Input + Processing + Output  Contains Code for Processing + Output 
Output of Python Program  Enter your marks in Python: 65.4 Your Grade in Python is: C  Enter your marks in Python: 65.4 Your Grade in Python is: C 
Execution of Input + Processing + Output  Input + Processing + Output is carried out in the Called Function (i.e., grade_calculation01() Function)  Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., grade_calculation02(marks) Function) 
Example 3  Solving Programming Tasks using Functions (with Function Prototype: Return Type, No Arguments / Parameters)
 Example  Steps (Solving Programming Tasks using Functions)
 Realworld Task
 Calculate Grade of Students
 In Sha Allah, in the next s we will solve the Realworld Task (i.e., Calculate Grade of Students) using Functions by following the Steps mentioned above
 Note
 The Function Prototype will be
 Return Type, No Arguments / Parameters
 The Function Prototype will be
 Example 3  Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the Realworld Task 
Step 1.1: Realworld Task 

Step 1.2: Realworld Task Description 

Step 1.3: Your Job – As a Software Developer 

Step 2: Can we treat the Realworld Task (Step 1.2) as a Programming Task? 
Step 2.1: Check Whether the Realworld Task can be treated as a Programming Task? 

Step 2.1.1: Write Down Input and Output of the Realworld Task 

Step 2.1.1.1: Example 1 – Input and Output of the Realworld Task 

Step 2.1.1.2: Example 2 – Input and Output of the Realworld Task 

Step 2.1.1.3: Example 3 – Input and Output of the Realworld Task 

Step 2.1.2: Conclusion 

Step 2.2: Convert the Realworld Task Description (Step 1.2) into Programming Task Description 

Step 3: Completely and Correctly Understand the Programming Task Description 
Step 3.1: Completely and Correctly Understand the Programming Task Description 

Step 3.2: Requirements Gathering 

Step 3.3: Requirements Analysis 

Step 4: Plan and Design Solution to the Programming Task 
Step 4.1: Identify InputProcessingOutput (from Steps 3.1, 3.2, 3.3) 

Step 4.2: Write down Function Prototype 

Step 4.3: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Programming Task 

Step 4.4: Convert Algorithm (Step 4.3) into Pseudo Code 
Pseudo Code Function main() FUNCTION CALL – result = grade_calculation3() DISPLAY – result End Function Function grade_calculation3() INPUT – marks IF marks >= 80 and marks <= 100 grade – A ELIF marks >= 70 and marks < 80 grade – B ELIF marks >= 60 and marks < 70 grade – C ELIF marks >= 50 and marks < 60 grade – D ELIF marks >= 0 and marks < 50 grade – F
DISPLAY – marks, grade RETURN – grade End Function 
Step 4.5: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 4.4) 
Step 5: Implementation 
'''
Program Details
__author_ = Ms. Samavi Salman
__copyright__ = Copyright (C) 2021 Ms. Samavi Suleman
__license__ = Public Domain
__program__name__ = Calculate Grade of a Student in Math
__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__ = 18062021
____End__Date___ = 21062021
'''
'''
Purpose of Program
The main purpose of this Python Program is to demonstrate how we can use Functions to calculate grade of a student in Math and display it on the Output Screen. Please note that the Function Prototype will be: Return Type, No Arguments / Parameters 😊
This program comprises of two Functions.
5. main()
6. grade_calculation3()
'''
# Function Prototypes for User Defined Functions
# char grade_calculation3(); # Function Prototype
def grade_calculation3():
try:
# Input
# Variable to store Input  marks
# Variable to store Output  grade
marks = float(input("Enter your marks in Math: "))
# Processing + Output
if marks >= 80 and marks <= 100:
grade = 'A'
elif marks >= 70 and marks < 80:
grade = 'B'
elif marks >= 60 and marks < 70:
grade = 'C'
elif marks >= 50 and marks < 60:
grade = 'D'
elif marks >= 0 and marks < 50:
grade = 'F'
# Display Input and Output
print("Your Marks in Math are: ",marks)
print("You Grade in Math is: ", grade)
# RETRUN result (Output) to the Calling Function
return grade;
except ValueError:
print("Error! Enter Integer/Float Value")
if __name__ == "__main__": # main() Function
result = grade_calculation3() # Function Call
# Display Output
print(result)
Enter your marks in Math: 70.5
Your Marks in Math are: 70.5
You Grade in Math is: B
Step 6: Dry Run the Python Program  
Python Statement  Processing  Memory  Output Screen 
Start (Program Execution) – Execution of Python Program starts from the main() Function. Therefore, Program Control will be with the main() Function 😊  
Program Control is with the main() Function  
result = grade_calculation3()  This Python Statement is an Assignment Statement and will be executed in the following Three Steps.
 result = (In main() Function)  – 
· Step 1: Execute Right Hand Side (RHS) of the Assignment Statement i.e., grade_calculation3(). Note that grade_calculation3() is a Function Call. · This Python Statement is a Function Call. Therefore, Program Control will move from Calling Function (main()) to the Called Function ( grade_calculation3()).  
Program Control is with the grade_calculation3() Function  
marks = float(input(“Enter your marks in Math: “))  Print Message on the Output Screen and take Marks as Input from User  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function)  Enter your marks in Math: 70.5 
The next Python Statement is a nested Selection Structure. There are two Possible Outcomes of a Condition: (1) True or (2) False.
 
IF marks >= 80 and marks <= 100:  Check whether the Condition is True or False
· 70.5 >= 80 and 70.5 <= 100 · False and False · False The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘A’) will be skipped and Program Control will move to the next Condition in the nested Selection Structure  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function)  Enter your marks in Math: 70.5 
grade – ‘A’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘A’) will be skipped  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function)  
ELIF marks >= 70 and marks < 80:  Check whether the Condition is True or False
· 70.5 >= 70 and 70.5 < 80 · True and True · True The Condition is True, Python Statements immediately after the Condition will be executed and Program Control will exit from the nested Selection Structure  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function)  Enter your marks in Math: 70.5 
grade – ‘B’  Store B in Variable grade (and exit from the nested Selection Structure)  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  Enter your marks in Math: 70.5 
ELIF marks >= 60 and marks < 70:  Check whether the Condition is True or False
· 70.5 >= 60 and 70.5 <= 70 · False and False · False The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘C’) will be skipped and Program Control will move to the next Condition in the nested Selection Structure  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  Enter your marks in Math: 70.5 
grade – ‘C’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘C’) will be skipped  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  
ELIF marks >= 50 and marks < 60:  Check whether the Condition is True or False
· 70.5 >= 50 and 70.5 < 60 · False and False · False The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘D’) will be skipped and Program Control will move to the next Condition in the nested Selection Structure  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  Enter your marks in Math: 70.5 
grade – ‘D’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘D’) will be skipped  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  Enter your marks in Math: 70.5 
ELIF marks >= 0 and marks < 50:  Check whether the Condition is True or False
· 70.5 >= 0 and 70.5 < 50 · False and False · False The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘F’) will be skipped and Program Control will move to the next Condition in the nested Selection Structure  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  Enter your marks in Math: 70.5 
grade – ‘F’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘F’) will be skipped  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  Enter your marks in Math: 70.5 
print(“Your Marks in Math are: “,marks)  Print Marks (Input) on the Output Screen.  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 
print(“You Grade in Math is: “, grade)  Print Grade (Output) on the Output Screen.  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
return grade  This Statement will RETURN the Value stored in Variable grade (i.e., B) to the Calling Function (i.e., main() Function)  result = (In main() Function) marks = 70.5 (In grade_calculation3() Function) grade = B (In grade_calculation3() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
Body of grade_calculation3() Function is completely executed. Variables declared in the grade_calculation1() Function will be destroyed and Program Control will move back from the Called Function ( grade_calculation3()) to the Calling Function (main())  
Program Control is with the main() Function  
–  –  result = B (In main() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
result = grade_calculation3()
 
result = grade_calculation3()  Store the Character (Output) Returned by grade_calculation3() in Variable result
· result = grade_calculation3() · result = B  result = B (In main() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
print(result)  Print Output (Grade) on the Output Screen.  result = B (In main() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
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. Variables declared in the main() Function will be destroyed and Python Program will be Terminated 😊  
–  –  –  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
Step 7: Analysis, Main Findings and Conclusion  
Step 7.1: Analysis of Python Program – Calling a Function (No Return Type, No Arguments / Parameters)  
 
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters)  
 
Step 7.3: Main Findings  
 
Step 7.4: Conclusion  

 Comparing Three Variations of Prototype
Analysis Parameters  No Return Type, No Arguments / Parameters  No Return Type, Arguments / Parameters  No Return Type, No Arguments / Parameters 
Function Prototype  void grade_calculation01();  void grade_calculation02(float marks)  void grade_calculation03(); 
Function Call  grade_calculation01();  grade_calculation02(float marks)  grade_calculation03(); 
Function Header  def grade_calculation01()  def grade_calculation02(marks)  def grade_calculation03() 
Body of Function  Contains Code for Input + Processing + Output  Contains Code for Processing + Output  Contains Code for Input + Processing + Output 
Return Value  N / A  N / A  Use return result to return one Integer Value return result; 
Output of Python Program  Enter your marks in Python: 65.4 Your Grade in Python is: C  Enter your marks in Python: 65.4 Your Grade in Python is: C  Enter your marks in Python: 65.4 Your Grade in Python is: C 
Execution of Input + Processing + Output  Input + Processing + Output is carried out in the Called Function (i.e., grade_calculation01() Function)  Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., grade_calculation02(marks) Function)  Input + Processing + Output is carried out in the Called Function (i.e., grade_calculation03() Function) 
Example 4  Solving Programming Tasks using Functions (with Function Prototype: Return Type, Arguments / Parameters)
 Example  Steps (Solving Programming Tasks using Functions)
 Realworld Task
 Calculate Grade of Students
 In Sha Allah, in the next s we will solve the Realworld Task (i.e., Calculate Grade of Students) using Functions by following the Steps mentioned above
 Note
 The Function Prototype will be
 Return Type, Arguments / Parameters
 The Function Prototype will be
 Example 4  Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the Realworld Task 
Step 1.1: Realworld Task 

Step 1.2: Realworld Task Description 

Step 1.3: Your Job – As a Software Developer 

Step 2: Can we treat the Realworld Task (Step 1.2) as a Programming Task? 
Step 2.1: Check Whether the Realworld Task can be treated as a Programming Task? 

Step 2.1.1: Write Down Input and Output of the Realworld Task 

Step 2.1.1.1: Example 1 – Input and Output of the Realworld Task 

Step 2.1.1.2: Example 2 – Input and Output of the Realworld Task 

Step 2.1.1.3: Example 3 – Input and Output of the Realworld Task 

Step 2.1.2: Conclusion 

Step 2.2: Convert the Realworld Task Description (Step 1.2) into Programming Task Description 

Step 3: Completely and Correctly Understand the Programming Task Description 
Step 3.1: Completely and Correctly Understand the Programming Task Description 

Step 3.2: Requirements Gathering 

Step 3.3: Requirements Analysis 

Step 4: Plan and Design Solution to the Programming Task 
Step 4.1: Identify InputProcessingOutput (from Steps 3.1, 3.2, 3.3) 

Step 4.2: Write down Function Prototype 

Step 4.3: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Programming Task 

Step 4.4: Convert Algorithm (Step 4.3) into Pseudo Code 
unction main() INPUT – marks FUNCTION CALL – result = grade_calculation4() DISPLAY – result End Function Function grade_calculation4() IF marks >= 80 and marks <= 100 grade – A ELIF marks >= 70 and marks < 80 grade – B ELIF marks >= 60 and marks < 70 grade – C ELIF marks >= 50 and marks < 60 grade – D ELIF marks >= 0 and marks < 50 grade – F
DISPLAY – marks, grade RETURN – grade End Function 
Step 4.5: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 4.4) 
Step 5: Implementation 
'''
Program Details
__author_ = Ms. Samavi Salman
__copyright__ = Copyright (C) 2021 Ms. Samavi Suleman
__license__ = Public Domain
__program__name__ = Calculate Grade of a Student in Math
__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__ = 18062021
____End__Date___ = 21062021
'''
'''
Purpose of Program
The main purpose of this Python Program is to demonstrate how we can use Functions to calculate grade of a student in Math and display it on the Output Screen. Please note that the Function Prototype will be: Return Type, Arguments / Parameters 😊
This program comprises of two Functions.
7. main()
8. grade_calculation4()
'''
# Function Prototypes for User Defined Functions
# char grade_calculation4(marks); # Function Prototype
def grade_calculation4(marks):
# Processing + Output
if marks >= 80 and marks <= 100:
grade = 'A'
elif marks >= 70 and marks < 80:
grade = 'B'
elif marks >= 60 and marks < 70:
grade = 'C'
elif marks >= 50 and marks < 60:
grade = 'D'
elif marks >= 0 and marks < 50:
grade = 'F'
# RETRUN Value (Output) to the Calling Function
return grade;
if __name__ == "__main__": # main() Function
try:
# Input
# Variable to store Input  marks
# Variable to store Output  grade
marks = float(input("Enter your marks in Math: "))
result = grade_calculation4(marks) # Function Call
# Display Input and Output
print("Your Marks in Math are: ",marks)
print("You Grade in Math is: ", result)
except ValueError:
print("Error! Enter Integer/Float Value")
Enter your marks in Math: 70.5
Your Marks in Math are: 70.5
You Grade in Math is: B
Step 6: Dry Run the Python Program 

Python Statement 
Processing 
Memory 
Output Screen 
Start (Program Execution) – Execution of Python Program starts from the main() Function. Therefore, Program Control will be with the main() Function 😊  
Program Control is with the main() Function  
marks = float(input(“Enter your marks in Math: “))  Print Message on the Output Screen and take Marks as Input from User  marks = 70.5 (In main() Function)  Enter your marks in Math: 70.5 
result=grade_calculation4(marks)  This Python Statement is an Assignment Statement and will be executed in the following Three Steps.

marks = 70.5 (In main() Function) result = (In main() Function)  Enter your marks in Math: 70.5 


Program Control is with the grade_calculation4() Function  
Function Header – grade_calculation4()  


–  –  marks = 70.5 (In main() Function) result = (In main() Function)  Enter your marks in Math: 70.5 
The next Python Statement is a nested Selection Structure. There are two Possible Outcomes of a Condition: (1) True or (2) False.


IF marks >= 80 and marks <= 100:  Check whether the Condition is True or False

marks = 70.5 (In main() Function) result = (In main() Function)  Enter your marks in Math: 70.5 
grade – ‘A’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘A’) will be skipped  marks = 70.5 (In main() Function) result = (In main() Function)  
ELIF marks >= 70 and marks < 80:  Check whether the Condition is True or False

marks = 70.5 (In main() Function) result = (In main() Function)  Enter your marks in Math: 70.5 
grade – ‘B’  Store B in Variable grade (and exit from the nested Selection Structure)  marks = 70.5 (In main() Function) result = (In main() Function) grade = B (In grade_calculation4() Function)  Enter your marks in Math: 70.5 
ELIF marks >= 60 and marks < 70:  Check whether the Condition is True or False

marks = 70.5 (In main() Function) result = (In main() Function) grade = B (In grade_calculation4() Function)  Enter your marks in Math: 70.5 
grade – ‘C’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘C’) will be skipped  marks = 70.5 (In main() Function) result = (In main() Function) grade = B (In grade_calculation4() Function)  
ELIF marks >= 50 and marks < 60:  Check whether the Condition is True or False

marks = 70.5 (In main() Function) result = (In main() Function) grade = B (In grade_calculation4() Function)  Enter your marks in Math: 70.5 
grade – ‘D’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘D’) will be skipped  marks = 70.5 (In main() Function) result = (In main() Function) grade = B (In grade_calculation4() Function)  Enter your marks in Math: 70.5 
ELIF marks >= 0 and marks < 50:  Check whether the Condition is True or False

marks = 70.5 (In main() Function) result = (In main() Function) grade = B (In grade_calculation4() Function)  Enter your marks in Math: 70.5 
grade – ‘F’  The Condition is False, Python Statements immediately after the Condition (i.e., grade = ‘F’) will be skipped  marks = 70.5 (In main() Function) result = (In main() Function) grade = B (In grade_calculation4() Function)  Enter your marks in Math: 70.5 
return grade  This Statement will RETURN the Value stored in Variable grade (i.e., B) to the Calling Function (i.e., main() Function)  marks = 70.5 (In main() Function) result = (In main() Function) grade = B (In grade_calculation4() Function)  Enter your marks in Math: 70.5 
Body of grade_calculation4() Function is completely executed. Variables declared in the grade_calculation4() Function will be destroyed and Program Control will move back from the Called Function ( grade_calculation4()) to the Calling Function (main())  
Program Control is with the main() Function  
–  –  marks = 70.5 (In main() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
result = grade_calculation4()


result = grade_calculation4(marks)  Store the Character (Output) Returned by grade_calculation4() in Variable result

marks = 70.5 (In main() Function) result = B (In main() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
print(“Your Marks in Math are: “,marks)  Print Marks (Input) on the Output Screen.  marks = 70.5 (In main() Function) result = B (In main() Function)  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 
print(“You Grade in Math is: “, grade)  Print Grade (Output) on the Output Screen.  marks = 70.5 (In main() Function) result = B (In main() Function))  Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B 
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. Variables declared in the main() Function will be destroyed and Python Program will be Terminated 😊 
Step 7: Analysis, Main Findings and Conclusion 

Step 7.1: Analysis of Python Program – Calling a Function (No Return Type, No Arguments / Parameters) 



Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters) 



Step 7.3: Main Findings 



Step 7.4: Conclusion 


 Comparing Four Variations of Prototype
Analysis Parameters  No Return Type, No Arguments / Parameters  No Return Type, Arguments / Parameters  No Return Type, No Arguments / Parameters  No Return Type, Arguments / Parameters 
Function Prototype  void grade_calculation01();  void grade_calculation02(float marks);  void grade_calculation03();  void grade_calculation04(float marks); 
Function Call  grade_calculation01();  grade_calculation02(marks)  grade_calculation03();  grade_calculation04(marks) 
Function Header  def grade_calculation01()  def grade_calculation02(marks)  def grade_calculation03()  void grade_calculation04(marks) 
Body of Function  Contains Code for Input + Processing + Output  Contains Code for Processing + Output  Contains Code for Input + Processing + Output  Contains Code for Processing + Output 
Return Value  N / A  N / A  Use return result to return one Integer Value return result;  Use return result to return one Integer Value return result; 
Output of Python Program  Enter your marks in Python: 65.4 Your Grade in Python is: C  Enter your marks in Python: 65.4 Your Grade in Python is: C  Enter your marks in Python: 65.4 Your Grade in Python is: C  Enter your marks in Python: 65.4 Your Grade in Python is: C 
Execution of Input + Processing + Output  Input + Processing + Output is carried out in the Called Function (i.e., grade_calculation01() Function)  Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., grade_calculation02(marks) Function)  Input + Processing + Output is carried out in the Called Function (i.e., grade_calculation03() Function)  Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., grade_calculation04(marks) Function) 
TODO and Your Turn
TODO Task 1
 Task
 Consider the following Tasks and answer the questions given below
 Note
 Your answer should be
 Well Justified
 Your answer should be
 Questions
 Write Function in Python Programs for all the Tasks given below by following the Steps given in this Lecture considering the following variation of Function Prototype i.e.,
 Solving Programming Tasks using Functions with Function Prototype:
 No Return Type, No Arguments / Parameters
 No Return Type, Arguments / Parameters
 Return Type, No Arguments / Parameters
 Return Type, Arguments / Parameters
 Solving Programming Tasks using Functions with Function Prototype:
 Write Function in Python Programs for all the Tasks given below by following the Steps given in this Lecture considering the following variation of Function Prototype i.e.,
Task 1 
Completely and Correctly Understand the Task 
Task Description 
· MasjideQuba 
Required Output 
The First Mosque built in Madina was: MasjideQuba 
Task 2 
Completely and Correctly Understand the Task 
Task Description 
· Hazrat Sarah A.S. 
Required Output 
First wife of Hazrat Ibrahim A.S. was: Hazrat Sarah A.S. 
Task 3 
Completely and Correctly Understand the Task 
Task Description 
· Hazrat Ibrahim (A.S) · Hazrat Ismail (A.S) 
Required Output 
Khana Kabba was first constructed by: Hazrat Ibrahim (A.S) and Hazrat Ismail (A.S) 
Your Turn Task 1
 Task
 Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
 For each Task write a Python Program using the following Steps mentioned in this Lecture considering the following variation of Function Prototype i.e.,
 Solving Programming Tasks using Functions with Function Prototype:
 No Return Type, No Arguments / Parameters
 No Return Type, Arguments / Parameters
 Return Type, No Arguments / Parameters
 Return Type, Arguments / Parameters
 Solving Programming Tasks using Functions with Function Prototype: