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 28, 2022
  • Home
  • Free Book

Table of Contents

Chapter 26 - A Step by Step Example to Write Functions II

  • Authors
  • Ms. Samavi Salman
  • Dr. Rao Muhammad Adeel Nawab
  • Supporting Material
  • Download Supporting Material (Code): here

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 Real-world Task
      • Step 1.1: Real-world Task
      • Step 1.2: Real-world Task Description
      • Step 1.3: Your Job – As a Software Developer
    • Step 2: Can we treat the Real-world Task (Step 1.2) as a Programming Task?
      • Step 2.1: Check Whether the Real-world Task can be treated as a Programming Task?
        • Step 2.1.1: Write Down Input and Output of the Real-world Task
          • Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
          • Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
          • Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
        • Step 2.1.2: Conclusion
      • Step 2.2: Convert the Real-world 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 Input-Processing-Output (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

Example 1 - Solving Programming Tasks using Functions (with Function Prototype: No Return Type, No Arguments / Parameters)

  • Example - Steps (Solving Programming Tasks using Functions)
  • Real-world Task
    • Calculate Grade of Students
  • In Sha Allah, in the next Slides we will solve the Real-world 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
  • Example 1 - Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the Real-world Task
Step 1.1: Real-world Task
  • Calculate Grade of a Student in Math
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants to calculate grade of her students in Math. For this purpose, she wants to develop a software 😊
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which calculates the Grade of a Student in Math and displays it on the Output Screen 😊
Step 2: Can we treat the Real-world Task (Step 1.2) as a Programming Task?
Step 2.1: Check Whether the Real-world Task can be treated as a Programming Task?
  • If (Real-world Task can be Broken into Input and Output)
    • THEN 
      • We MAY Treat Real-world Task as a Programming Task
    • ELSE
      • We CANNOT Treat Real-world Task as a Programming Task
Step 2.1.1: Write Down Input and Output of the Real-world Task
  • Input
    • Marks (in Math) 
  • Output
    • Grade 
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Marks = 80.2
    • Output
      • A
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Marks = 47
    • Output
      • F
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Marks =65.0
    • Output
      • C
Step 2.1.2: Conclusion
  • Yes, we MAY Treat Real-world Task as a Programming Task
Step 2.2: Convert the Real-world Task Description (Step 1.2) into Programming Task Description
  • Write a Python Program that takes Marks (either as Integer or Float) of a Student as   Input from User, calculates the Grade (Output) and displays it on the Output Screen.
  • Note – Please implement this using a Function (called grade_calculation1()). The Function Prototype should be: No Return Type, No Arguments / Parameters 😊
Step 3: Completely and Correctly Understand the Programming Task Description
Step 3.1: Completely and Correctly Understand the Programming Task Description
  • The main objective of this Software is to calculate Grade of a Student in Math and display it on the Output Screen
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 1
      • Take Marks (Integer / Float) as Input from User 
    • Requirement 2
      • Display the Grade of Student on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Take Marks (Integer / Float) as Input from User
    • Marks can be entered as either Integer or Float by the User
  • Analysis of Requirement 2 – Display the Grade of Student on the Output Screen
    • No need to Print or Save Output in any Format
    • Just Display it on the Output Screen
Step 4: Plan and Design Solution to the Programming Task
Step 4.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3)
  • Input
    • Data
      • Marks 
    • Instruction(s)
      • Calculate Grade of Student
      • Display Grade of Student on the Output Screen
  • Output
    • A / B / C / D / F
  • Processing
    • Use Conditions for Decision Making – To calculate Grade of Student based on Marks, check the following conditions?
      • Condition for A Grade – marks >= 80 and marks <= 100
      • Condition for B Grade – marks >= 70 and marks < 80
      • Condition for C Grade – marks >= 60 and marks < 70
      • Condition for D Grade – marks >= 50 and marks < 60
      • Condition for F Grade – marks >= 0 and marks < 50
    • Use print() Function  – To display the Messages and Data Values on the Output Screen
  •  
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 1
    • Data Type of Input = Integer / Float
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = String
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be grade_calculation1() and Function Prototype should have: No Return Type, No Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • void grade_calculation1();
Step 4.3: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Programming Task
  • Algorithm
    • The main() Function will call the grade_calculation1() Function, which will take Marks (either as Integer or Float) as Input from User, calculate the Grade of Student and display the result (Output) on the Output Screen.
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__          = 18-06-2021
____End__Date___          = 21-06-2021
'''
				
			
				
					'''
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 the Condition is True, Python Statements immediately after the Condition will be executed and Program Control will exit from the nested Selection Structure
  • If the Condition is False, Python Statements immediately after the Condition will be skipped and Program Control will move to the next Condition in the nested Selection Structure

IF marks >= 80 and marks <= 100:

Check whether the Condition is True or False

  • Preprocessing

·        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

  • Preprocessing

·        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

  • Preprocessing

·        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

  • Preprocessing

·        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

  • Preprocessing

·        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)
  • Function Prototype 
    • void grade_calculation1();
  • Function Header
    • def grade_calculation1():   
  • Function Call
    • grade_calculation1()
  • Analysis 
    • The grade_calculation1() Function does not take any Arguments / Parameters and does not Return any Value, which is evident from 
      • () Brackets 
  • In Function Prototype, Function Header and Function Call, we have () Brackets i.e., 
    • Function Header and Function Call Matches Function Prototype 
  • Conclusion 
    • When a Function does not take any Arguments / Parameters and does not Return any Value, in Function Call we use
      • Function Name with ()
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters)
Analysis Parameters
Function Prototype – No Return Type, No Arguments / Parameters
Function Prototype

void grade_calculation1();

Function Call

grade_calculation1();

Function Header 

def grade_calculation1()

Body of Function 

Contains Code for Input + Processing + Output 

Output of Python Program

Enter your marks in Math: 70.5

Your Marks in Math are: 70.5

You Grade in Math is: B

Execution of Input + Processing + Output

Input + Processing + Output is carried out in the Called Function (i.e., grade_calculation1() Function)

Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, grade_calculation1() Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Grade of a Student in Math on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.
    • Calculating and Displaying Grade of a Student in Math on the Output Screen was successfully accomplished using grade_calculation1() Function. Alhamdulillah (الحمدللہ) 

 

Example 2 - Solving Programming Tasks using Functions (with Function Prototype: No Return Type, Arguments / Parameters)

  • Example - Steps (Solving Programming Tasks using Functions)
  • Real-world Task
    • Calculate Grade of Students
  • In Sha Allah, in the next s we will solve the Real-world 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
  • Example 2 - Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the Real-world Task
Step 1.1: Real-world Task
  • Calculate Grade of a Student in Math
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants to calculate grade of her students in Math. For this purpose, she wants to develop a software 😊
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which calculates the Grade of a Student in Math and displays it on the Output Screen 😊
Step 2: Can we treat the Real-world Task (Step 1.2) as a Programming Task?
Step 2.1: Check Whether the Real-world Task can be treated as a Programming Task?
  • If (Real-world Task can be Broken into Input and Output)
    • THEN 
      • We MAY Treat Real-world Task as a Programming Task
    • ELSE
      • We CANNOT Treat Real-world Task as a Programming Task
Step 2.1.1: Write Down Input and Output of the Real-world Task
  • Input
    • Marks (in Math) 
  • Output
    • Grade 
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Marks = 80.2
    • Output
      • A
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Marks = 47
    • Output
      • F
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Marks =65.0
    • Output
      • C
Step 2.1.2: Conclusion
  • Yes, we MAY Treat Real-world Task as a Programming Task
Step 2.2: Convert the Real-world Task Description (Step 1.2) into Programming Task Description
  • Write a Python Program takes Marks (either as Integer or Float) of a Student as   Input from User, calculates the Grade (Output) and displays it on the Output Screen.
  • Note – Please implement this using a Function (called grade_calculation2(marks)). The Function Prototype should be: No Return Type, Arguments / Parameters 😊
Step 3: Completely and Correctly Understand the Programming Task Description
Step 3.1: Completely and Correctly Understand the Programming Task Description
  • The main objective of this Software is to calculate Grade of a Student in Math and display it on the Output Screen
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 1
      • Take Marks (Integer / Float) as Input from User 
    • Requirement 2
      • Display the Grade of Student on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Take Marks (Integer / Float) as Input from User
    • Marks can be entered as either Integer or Float by the User
  • Analysis of Requirement 2 – Display the Grade of Student on the Output Screen
    • No need to Print or Save Output in any Format
    • Just Display it on the Output Screen
Step 4: Plan and Design Solution to the Programming Task
Step 4.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3)
  • Input
    • Data
      • Marks 
    • Instruction(s)
      • Calculate Grade of Student
      • Display Grade of Student on the Output Screen
  • Output
    • A / B / C / D / F
  • Processing
    • Use Conditions for Decision Making – To calculate Grade of Student based on Marks, check the following conditions?
      • Condition for A Grade – marks >= 80 and marks <= 100
      • Condition for B Grade – marks >= 70 and marks < 80
      • Condition for C Grade – marks >= 60 and marks < 70
      • Condition for D Grade – marks >= 50 and marks < 60
      • Condition for F Grade – marks >= 0 and marks < 50
    • Use print() Function  – To display the Messages and Data Values on the Output Screen
  •  
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 1
    • Data Type of Input = Integer / Float
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = String
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be grade_calculation2(marks) and Function Prototype should have: No Return Type, Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • void grade_calculation2(marks);
Step 4.3: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Programming Task
  • Algorithm
    • The main() Function will call the grade_calculation2() Function and pass Marks (either as Integer or Float) as Argument / Parameter to it. The grade_calculation2() Function, will receive the Argument / Parameter passed to it, calculate the Grade of Student and display the result (Output) on the Output Screen.
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__          = 18-06-2021
____End__Date___          = 21-06-2021
'''
				
			
				
					'''
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()
  • Program Control is with the grade_calculation2() Function 
·
    • Function Header 
· o
      • Step 1: Declare a Float/ Integer Variable marks 
      • Step 2: Store Value (i.e., 70.5) Passed by Calling Function (i.e., main() Function) in the Variable marks (declared in Step 1)
– – 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 the Condition is True, Python Statements immediately after the Condition will be executed and Program Control will exit from the nested Selection Structure
  • If the Condition is False, Python Statements immediately after the Condition will be skipped and Program Control will move to the next Condition in the nested Selection Structure
IF marks >= 80 and marks <= 100: Check whether the Condition is True or False
  • Preprocessing
·        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  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
  • Preprocessing
·        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 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
  • Preprocessing
·        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  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
  • Preprocessing
·        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  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
  • Preprocessing
·        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  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)
  • Function Prototype 
    • void grade_calculation2(marks);
  • Function Header
    • def grade_calculation2(marks);
  • Function Call
    • grade_calculation2(marks);
  • Analysis 
    • The grade_calculation2(marks) Function takes an Argument / Parameter and does not Return any Value, which is evident from 
      • () Brackets 
    • In Function Prototype, Function Header and Function Call, we have () Brackets i.e., 
      • Function Header and Function Call Matches Function Prototype 
  • Conclusion 
    • When a Function takes an Argument / Parameter and does not Return any Value, in Function Call we use
      • Function Name with (marks)
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters)
Analysis Parameters
Function Prototype – No Return Type, No Arguments / Parameters
Function Prototype
void grade_calculation2(marks);
Function Call
grade_calculation2(marks);
Function Header
def grade_calculation2(marks);
Body of Function
Contains Code for Input + Processing + Output 
Output of Python Program
Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B
Execution of Input + Processing + Output
Input + Processing + Output is carried out in the Called Function (i.e., grade_calculation1() Function)
Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, grade_calculation2(marks) Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Grade of a Student in Math on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.
    • Calculating and Displaying Grade of a Student in Math on the Output Screen was successfully accomplished using grade_calculation2(marks) Function. Alhamdulillah (الحمدللہ) 
  • Comparing Two Variations of Prototype 
Analysis Parameters
No Return Type, No Arguments / Parameters
No Return Type, Arguments / Parameters
Function Prototypevoid grade_calculation01();void grade_calculation02(float marks)
Function Callgrade_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 + OutputInput + 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)
  • Real-world Task
    • Calculate Grade of Students
  • In Sha Allah, in the next s we will solve the Real-world 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
  • Example 3 - Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the Real-world Task
Step 1.1: Real-world Task
  • Calculate Grade of a Student in Math
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants to calculate grade of her students in Math. For this purpose, she wants to develop a software 😊
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which calculates the Grade of a Student in Math and displays it on the Output Screen 😊
Step 2: Can we treat the Real-world Task (Step 1.2) as a Programming Task?
Step 2.1: Check Whether the Real-world Task can be treated as a Programming Task?
  • If (Real-world Task can be Broken into Input and Output)
    • THEN 
      • We MAY Treat Real-world Task as a Programming Task
    • ELSE
      • We CANNOT Treat Real-world Task as a Programming Task
Step 2.1.1: Write Down Input and Output of the Real-world Task
  • Input
    • Marks (in Math) 
  • Output
    • Grade 
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Marks = 80.2
    • Output
      • A
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Marks = 47
    • Output
      • F
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Marks =65.0
    • Output
      • C
Step 2.1.2: Conclusion
  • Yes, we MAY Treat Real-world Task as a Programming Task
Step 2.2: Convert the Real-world Task Description (Step 1.2) into Programming Task Description
  • Write a Python Program takes Marks (either as Integer or Float) of a Student as   Input from User, calculates the Grade (Output) and displays it on the Output Screen. 
  • Note – Please implement this using a Function (called grade_calculation3()). The Function Prototype should be: Return Type, No Arguments / Parameters 😊
Step 3: Completely and Correctly Understand the Programming Task Description
Step 3.1: Completely and Correctly Understand the Programming Task Description
  • The main objective of this Software is to calculate Grade of a Student in Math and display it on the Output Screen
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 1
      • Take Marks (Integer / Float) as Input from User 
    • Requirement 2
      • Display the Grade of Student on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Take Marks (Integer / Float) as Input from User
    • Marks can be entered as either Integer or Float by the User
  • Analysis of Requirement 2 – Display the Grade of Student on the Output Screen
    • No need to Print or Save Output in any Format
    • Just Display it on the Output Screen
Step 4: Plan and Design Solution to the Programming Task
Step 4.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3)
  • Input
    • Data
      • Marks 
    • Instruction(s)
      • Calculate Grade of Student
      • Display Grade of Student on the Output Screen
  • Output
    • A / B / C / D / F
  • Processing
    • Use Conditions for Decision Making – To calculate Grade of Student based on Marks, check the following conditions?
      • Condition for A Grade – marks >= 80 and marks <= 100
      • Condition for B Grade – marks >= 70 and marks < 80
      • Condition for C Grade – marks >= 60 and marks < 70
      • Condition for D Grade – marks >= 50 and marks < 60
      • Condition for F Grade – marks >= 0 and marks < 50
    • Use print() Function  – To display the Messages and Data Values on the Output Screen
  •  
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 1
    • Data Type of Input = Integer / Float
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = String
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be grade_calculation3() and Function Prototype should have: Return Type, No Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • char grade_calculation3();
Step 4.3: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Programming Task
  • Algorithm
    • The main() Function will call the grade_calculation3() Function, which will take Marks (either as Integer or Float) as Input from User, calculate the Grade of Student and display the result (Output) on the Output Screen.
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__          = 18-06-2021
____End__Date___          = 21-06-2021
'''
				
			
				
					'''
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.

  • Step 1:  Execute Right Hand Side (RHS) of the Assignment Statement i.e.,  grade_calculation3(). Note that   grade_calculation3() is a Function Call.
  • Step 2:  Once Function Call (i.e.,  grade_calculation3()) is complete, it will return a character on RHS OF THE Assignment Statement i.e.,  grade_calculation3() will be replaced with the Returned Value
  • Step 3:  Store the Returned Value (Step 2) in the Variable result on the Left Hand Side (LHS) of the Assignment Statement

result =

(In main() Function)

–

  • result =  grade_calculation3()

·        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 the Condition is True, Python Statements immediately after the Condition will be executed and Program Control will exit from the nested Selection Structure
  • If the Condition is False, Python Statements immediately after the Condition will be skipped and Program Control will move to the next Condition in the nested Selection Structure

IF marks >= 80 and marks <= 100:

Check whether the Condition is True or False

  • Preprocessing

·        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

  • Preprocessing

·        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

  • Preprocessing

·        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

  • Preprocessing

·        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

  • Preprocessing

·        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()

  • Step 1:  Once Function Call (i.e.,  grade_calculation3()) is complete, it will return a Float value on RHS of the Assignment Statement i.e.,  grade_calculation3() will be replaced with the Returned Value
  • result = B
  • Step 2:  Store the Returned Value (Step 2) in the Variable result on the Left Hand Side (LHS) of the Assignment Statement

result =  grade_calculation3()

Store the Character (Output) Returned by  grade_calculation3() in Variable result

  • Calculation

·        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)
  • Note the Function Prototype, Function Header and Function Call 
  • Function Prototype 
    • char grade_calculation3();
  • Function Header
    • def grade_calculation3():   
  • Function Call
    • grade_calculation3()
  • Analysis 
    • The grade_calculation3() Function does not take any Argument / Parameter but Return a Value, which is evident from 
      • RETURN statement
  • In Function Prototype, Function Header and Function Call, we have () Brackets i.e., 
    • Function Header and Function Call Matches Function Prototype 
  • Conclusion 
    • When a Function does not take any Arguments / Parameters but Return any Value, in Function Call we use
      • Function Name with ()
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters)
Analysis Parameters
Function Prototype – No Return Type, No Arguments / Parameters
Function Prototype

char grade_calculation3();

Function Call

grade_calculation3();

Function Header 

def grade_calculation3()

Body of Function 

Contains Code for Input + Processing + Output 

Output of Python Program

Enter your marks in Math: 70.5

Your Marks in Math are: 70.5

You Grade in Math is: B

Execution of Input + Processing + Output

Input + Processing + Output is carried out in the Called Function (i.e., grade_calculation1() Function)

Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, grade_calculation3() Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Grade of a Student in Math on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.
    • Calculating and Displaying Grade of a Student in Math on the Output Screen was successfully accomplished using grade_calculation3() Function. Alhamdulillah (الحمدللہ) 

 

  • 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)
  • Real-world Task
    • Calculate Grade of Students
  • In Sha Allah, in the next s we will solve the Real-world 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
  • Example 4 - Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the Real-world Task
Step 1.1: Real-world Task
  • Calculate Grade of a Student in Math
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants to calculate grade of her students in Math. For this purpose, she wants to develop a software 😊
Step 1.3: Your Job – As a Software Developer
  • Your Job as a Software Developer is to write a Python Program for Ms. Samavi, which calculates the Grade of a Student in Math and displays it on the Output Screen 😊
Step 2: Can we treat the Real-world Task (Step 1.2) as a Programming Task?
Step 2.1: Check Whether the Real-world Task can be treated as a Programming Task?
  • If (Real-world Task can be Broken into Input and Output)
    • THEN 
      • We MAY Treat Real-world Task as a Programming Task
    • ELSE
      • We CANNOT Treat Real-world Task as a Programming Task
Step 2.1.1: Write Down Input and Output of the Real-world Task
  • Input
    • Marks (in Math) 
  • Output
    • Grade 
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Marks = 80.2
    • Output
      • A
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Marks = 47
    • Output
      • F
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Marks =65.0
    • Output
      • C
Step 2.1.2: Conclusion
  • Yes, we MAY Treat Real-world Task as a Programming Task
Step 2.2: Convert the Real-world Task Description (Step 1.2) into Programming Task Description
  • Write a Python Program takes Marks (either as Integer or Float) of a Student as   Input from User, calculates the Grade (Output) and displays it on the Output Screen.
  • Note – Please implement this using a Function (called grade_calculation4(marks)). The Function Prototype should be: Return Type, Arguments / Parameters 😊
Step 3: Completely and Correctly Understand the Programming Task Description
Step 3.1: Completely and Correctly Understand the Programming Task Description
  • The main objective of this Software is to calculate Grade of a Student in Math and display it on the Output Screen
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 1
      • Take Marks (Integer / Float) as Input from User 
    • Requirement 2
      • Display the Grade of Student on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Take Marks (Integer / Float) as Input from User
    • Marks can be entered as either Integer or Float by the User
  • Analysis of Requirement 2 – Display the Grade of Student on the Output Screen
    • No need to Print or Save Output in any Format
    • Just Display it on the Output Screen
Step 4: Plan and Design Solution to the Programming Task
Step 4.1: Identify Input-Processing-Output (from Steps 3.1, 3.2, 3.3)
  • Input
    • Data
      • Marks 
    • Instruction(s)
      • Calculate Grade of Student
      • Display Grade of Student on the Output Screen
  • Output
    • A / B / C / D / F
  • Processing
    • Use Conditions for Decision Making – To calculate Grade of Student based on Marks, check the following conditions?
      • Condition for A Grade – marks >= 80 and marks <= 100
      • Condition for B Grade – marks >= 70 and marks < 80
      • Condition for C Grade – marks >= 60 and marks < 70
      • Condition for D Grade – marks >= 50 and marks < 60
      • Condition for F Grade – marks >= 0 and marks < 50
    • Use print() Function  – To display the Messages and Data Values on the Output Screen
  •  
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 1
    • Data Type of Input = Integer / Float
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = String
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be grade_calculation4(marks) and Function Prototype should have: Return Type, Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • char grade_calculation4(marks);
Step 4.3: Write Down the Algorithm for the Proposed Solution (i.e., Software) to Solve the Programming Task
  • Algorithm
    • The main() Function will call the grade_calculation4() Function and pass Marks (either as Integer or Float) as Argument / Parameter to it. The grade_calculation4() Function, will receive the Argument / Parameter passed to it, calculate the Grade of Student. The grade_calculation4() Function will return the result to the main() Function. The main() Function will receive the result returned by the grade_calculation4() Function and display it on the Output Screen.
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__          = 18-06-2021
____End__Date___          = 21-06-2021
'''
				
			
				
					'''
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.
  • Step 1:  Execute Right Hand Side (RHS) of the Assignment Statement i.e., grade_calculation4(). Note that   grade_calculation4() is a Function Call.
  • Step 2:  Once Function Call (i.e., grade_calculation4()) is complete, it will return a character on RHS OF THE Assignment Statement i.e.,  grade_calculation4() will be replaced with the Returned Value
  • Step 3:  Store the Returned Value (Step 2) in the Variable result on the Left Hand Side (LHS) of the Assignment Statement
marks = 70.5 (In main() Function) result =  (In main() Function) Enter your marks in Math: 70.5
  • result = grade_calculation4()
·        Step 1:  Execute Right Hand Side (RHS) of the Assignment Statement i.e., grade_calculation4(). Note that grade_calculation4() 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_calculation4()).
Program Control is with the  grade_calculation4() Function
Function Header –  grade_calculation4()
  • Program Control is with the grade_calculation4() Function 
·
    • Function Header 
· o
      • Step 1: Declare a Float/ Integer Variable marks 
      • Step 2: Store Value (i.e., 70.5) Passed by Calling Function (i.e., main() Function) in the Variable marks (declared in Step 1)
– – 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 the Condition is True, Python Statements immediately after the Condition will be executed and Program Control will exit from the nested Selection Structure
  • If the Condition is False, Python Statements immediately after the Condition will be skipped and Program Control will move to the next Condition in the nested Selection Structure
IF marks >= 80 and marks <= 100: Check whether the Condition is True or False
  • Preprocessing
·        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 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
  • Preprocessing
·        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 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
  • Preprocessing
·        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 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
  • Preprocessing
·        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  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
  • Preprocessing
·        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 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()
  • Step 1:  Once Function Call (i.e., grade_calculation4()) is complete, it will return a Float value on RHS of the Assignment Statement i.e., grade_calculation4() will be replaced with the Returned Value
  • result = B
  • Step 2:  Store the Returned Value (Step 2) in the Variable result on the Left Hand Side (LHS) of the Assignment Statement
result = grade_calculation4(marks) Store the Character (Output) Returned by grade_calculation4() in Variable result
  • Calculation
·        result = grade_calculation4() ·        result = B
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)
  • Function Prototype 
    • char grade_calculation4(marks);
  • Function Header
    • def grade_calculation4(marks):   
  • Function Call
    • grade_calculation4(marks)
  • Analysis 
    • The grade_calculation4(marks) Function takes an Arguments / Parameters and Return a Value, which is evident from 
      • RETURN statement 
  • In Function Prototype, Function Header and Function Call, we have () Brackets i.e., 
    • Function Header and Function Call Matches Function Prototype 
  • Conclusion 
    • When a Function takes an Arguments / Parameters and Return a Value, in Function Call we use
      • Function Name with (marks)
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters)
Analysis Parameters
Function Prototype – No Return Type, No Arguments / Parameters
Function Prototype
char grade_calculation4(marks);
Function Call
 grade_calculation4(marks)
Function Header
def grade_calculation4(marks)
Body of Function
Contains Code for Input + Processing + Output 
Output of Python Program
Enter your marks in Math: 70.5 Your Marks in Math are: 70.5 You Grade in Math is: B
Execution of Input + Processing + Output
Input + Processing + Output is carried out in the Called Function (i.e., grade_calculation1() Function)
Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, grade_calculation4(marks) Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Grade of a Student in Math on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.
    • Calculating and Displaying Grade of a Student in Math on the Output Screen was successfully accomplished using grade_calculation4(marks) Function. Alhamdulillah (الحمدللہ) 
  • 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 Prototypevoid grade_calculation01();void grade_calculation02(float marks);void grade_calculation03();void grade_calculation04(float marks);
Function Callgrade_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 ValueN / AN / AUse 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 + OutputInput + 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 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 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

 

                        Task 1

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called Mosque_in_Madina()) which takes String as Input from User and prints the message on the Output Screen. 

·        Masjid-e-Quba

Required Output

The First Mosque built in Madina was: Masjid-e-Quba

 

                        Task 2

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called Family()) which takes String as Input from User and prints the message on the Output Screen. 

·        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

  • Write a Function (called Construction_of_Kabba()) which takes String as Input from User and prints the message on the Output Screen. 

·        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 Tasks

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
Chapter 25- A Step by Step Example to Write Functions I
  • Previous
Chapter 27 - A Step by Step Example to Write Functions III
  • 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.