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 27 - A Step by Step Example to Write Functions III

  • 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 the Sum of three Float Numbers 
    • In Sha Allah, in the next s we will solve the Real-world Task (i.e., Calculate the Sum of three Float Numbers) 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 the Sum of Three Float Numbers 
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants them to practice the concept of adding three Float Numbers. For this purpose, she wants to develop a software for her students 😊
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 Sum of three Float Numbers 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
    • Float Number 1 
    • Float Number 2
    • Float Number 3 
  • Output
    • Float Number 1 + Float Number 2 + Float Number 3
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Number1 = 10.25
      • Number2 = 15.30
      • Number3 = 20.55
    • Output
      • 46.1
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Number1 = 2.45
      • Number2 = 6.15
      • Number3 = 9.00
    • Output
      • 17.6
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Number1 = 200.0
      • Number2 = 600.1
      • Number3 = 575.2
    • Output
      • 1375.3
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 three Float Numbers as Input from User, calculates their sum and displays the result (Output) on the Output Screen.
    Note – Please implement this using a Function (called add1()). 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 sum of three Float Numbers and display the result on the Output Screen.
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 1
      • Only take three Float Numbers as Input from User 
    • Requirements 2
      • Display the Sum of Three Float Numbers on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Only take Float Numbers as Input from User
    • Only take Float Number as Input from User (Integers are also allowed)
  • Analysis of Requirement 2 – Display the Sum of three Float Numbers 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
      • Three Float Numbers (called number1, number2 and number3)
    • Instruction(s)
      • Calculate Sum of Three Float Numbers
      • Display Sum of Three Float Numbers on the Output Screen
  • Output
    • number1 +  number2 + number3 
  • Processing
    • Use + Operator to calculate Sum of Three Float Numbers – Add all three Float Numbers
    • Use print() Function  – To display the Sum of Three Float Numbers on the Output Screen
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 3
      • Number 1
      • Number 2
      • Number 3
    • Data Type of Input(s)
      • Data Type of Number 1 = Float
      • Data Type of Number 2 = Float
      • Data Type of Number 3 = Float
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = Float
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be add1() and Function Prototype should have: No Return Type, No Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • void add1();
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 add1() Function, which will take three Float Numbers as Input from User, calculate its sum and displays the result  (Output) on the Output Screen.
Step 4.4: Convert Algorithm (Step 4.3) into Pseudo Code

Function main()

          FUNCTION CALL – add1()

End Function

Function add1()

         INPUT – number1

         INPUT – number2

         INPUT – number3

         CALCULATE – sum = number1 + number2 + number3 

         DISPLAY – number1, Number 2, number3 and sum

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 Sum of three Float Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 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 sum of three Float Numbers and display the result (Output) 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.	add1()
'''
# Function Prototypes for User Defined Functions 
# void add1(); Function Prototype
def add1():
    try:
        # Input
        # Variable to store Inputs – number1, number2 and number3
        # Variable to store Output - sum
        number1 = float(input("Enter first Float Number: "))
        number2 = float(input("Enter second Float Number: "))
        number3 = float(input("Enter third Float Number: "))
    
        # Processing + Output 
        sum = number1 + number2 + number3
    
        # Display Input and Output
        print(number1,"+",number2,"+",number3, "=", sum)
    except ValueError:
        print("Error! Enter Integer Value")
if __name__ == "__main__":  # main() Function 
    add1() # Function Call
				
			
				
					Enter first Float Number: 21.2
Enter second Float Number: 15.3
Enter third Float Number: 15
21.2 + 15.3 + 15.0 = 51.5
				
			
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

add1()

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

–

–

Program Control is with the add1() Function

number1 = float(input(“Enter first Float Number: “))

    

Print Message on the Output Screen and take first Float Number as Input from User 

number1 = 21.2

(In add1() Function)

Enter first Float Number: 21.2

number2 = float(input(“Enter second Float Number: “))

    

Print Message on the Output Screen and take second Float Number as Input from User 

number1 = 21.2

(In add1() Function)

number2 = 15.3

(In add1() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

number3 = float(input(“Enter third Float Number: “))

Print Message on the Output Screen and take third Float Number as Input from User 

number1 = 21.2

(In add1() Function)

number2 = 15.3

(In add1() Function)

number3 = 15

(In add1() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

sum = number1 + number2 + number3

Add all three Float numbers (to calculate their sum) and store result (Output) in a variable (called sum)

  • Calculation

·        sum = number1 + number2 + number3

·        sum = 21.2 + 15.3 + 15.0 = 51.5

·        sum = 51.5

  • Note

·        In the calculation we have used the Variable number1, number2 and number3 (In add1() Function)

number1 = 21.2

(In add1() Function)

number2 = 15.3

(In add1() Function)

number3 = 15

(In add1() Function)

sum = 51.5

(In add1() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

print(number1,”+”,number2,”+”,number3, “=”, sum)

Print three Float numbers (Input) and their sum (Output) on the Output Screen.

number1 = 21.2

(In add1() Function)

number2 = 15.3

(In add1() Function)

number3 = 15

(In add1() Function)

sum = 51.5

(In add1() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

Body of add1() Function is completely executed. Variables declared in the add1() Function will be destroyed and Program Control will move back from the Called Function (add1()) to the Calling Function (main())

Program Control is with the main() Function

–

–

–

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

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 first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

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 
    • void add1();
  • Function Header
    • def add1():   
  • Function Call
    • add1()
  • Analysis 
    • The add1() 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 add1();
Function Call
add1();
Function Header 
def add1();
Body of Function 
Contains Code for Input + Processing + Output 
Output of Python Program

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

Execution of Input + Processing + Output
Input + Processing + Output is carried out in the Called Function (i.e., square01() Function)
Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, add1() Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Sum of three Float Numbers on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.
    • Calculating and Displaying Sum of three Float Numbers on the Output Screen was successfully accomplished using add1() 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 Sum of three Float Numbers 
  • In Sha Allah, in the next s we will solve the Real-world Task (i.e., Calculate Sum of three Float Numbers) 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 the Sum of Three Float Numbers 
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants them to practice the concept of adding three Float Numbers. For this purpose, she wants to develop a software for her students 😊
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 Sum of three Float Numbers 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
    • Float Number 1 
    • Float Number 2
    • Float Number 3 
  • Output
    • Float Number 1 + Float Number 2 + Float Number 3
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Number1 = 10.25
      • Number2 = 15.30
      • Number3 = 20.55
    • Output
      • 46.1
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Number1 = 2.45
      • Number2 = 6.15
      • Number3 = 9.00
    • Output
      • 17.6
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Number1 = 200.0
      • Number2 = 600.1
      • Number3 = 575.2
    • Output
      • 1375.3
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 three Float Numbers as Input from User, calculates their sum and displays the result (Output) on the Output Screen.
  • Note – Please implement this using a Function (called add2()). 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 sum of three Float Numbers and display the result on the Output Screen.
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 1
      • Only take three Float Numbers as Input from User 
    • Requirements 2
      • Display the Sum of Three Float Numbers on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Only take Float Numbers as Input from User
    • Only take Float Number as Input from User (Integers are also allowed)
  • Analysis of Requirement 2 – Display the Sum of three Float Numbers 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
      • Three Float Numbers (called number1, number2 and number3)
    • Instruction(s)
      • Calculate Sum of Three Float Numbers
      • Display Sum of Three Float Numbers on the Output Screen
  • Output
    • number1 +  number2 + number3 
  • Processing
    • Use + Operator to calculate Sum of Three Float Numbers – Add all three Float Numbers
    • Use print() Function  – To display the Sum of Three Float Numbers on the Output Screen
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 3
      • Number 1
      • Number 2
      • Number 3
    • Data Type of Input(s)
      • Data Type of Number 1 = Float
      • Data Type of Number 2 = Float
      • Data Type of Number 3 = Float
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = Float
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be add2() and Function Prototype should have: No Return Type, Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • void add2();
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 add2() Function and pass three Float Number as Argument / Parameter to it. The add2() Function, will receive the Argument / Parameter passed to it, calculate its sum and display the result on the Output Screen.
Step 4.4: Convert Algorithm (Step 4.3) into Pseudo Code

Pseudo Code

Function main()

         INPUT – number1

         INPUT – number2

         INPUT – number3

         FUNCTION CALL – add2()

End Function

Function add2()

         CALCULATE – sum = number1 + number2 + number3 

         DISPLAY – number1, Number 2, number3 and sum

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 Sum of three Float Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 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 sum of three Float Numbers and display the result (Output) 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.      add2()
'''
# Function Prototypes for User Defined Functions 
# void add2(number1, number2, number3); Function Prototype
def add2(number1, number2, number3):  
    # Processing + Output 
    sum = number1 + number2 + number3
    
    # Display Input and Output
    print(number1,"+",number2,"+",number3, "=", sum)
    
if __name__ == "__main__":  # main() Function 
    try:
        # Input
        # Variable to store Inputs – number1, number2 and number3
        # Variable to store Output - sum
        number1 = float(input("Enter first Float Number: "))
        number2 = float(input("Enter second Float Number: "))
        number3 = float(input("Enter third Float Number: "))
        add2(number1, number2, number3) # Function Call
    except ValueError:
        print("Error! Enter Integer Value")

				
			
				
					Enter first Float Number: 21.2
Enter second Float Number: 15.3
Enter third Float Number: 15
21.2 + 15.3 + 15.0 = 51.5
				
			
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

number1 = float(input(“Enter first Float Number: “))

    

Print Message on the Output Screen and take first Float Number as Input from User 

number1 = 21.2

(In main() Function)

Enter first Float Number: 21.2

number2 = float(input(“Enter second Float Number: “))

    

Print Message on the Output Screen and take second Float Number as Input from User 

number1 = 21.2

(In main() Function)

number2 = 15.3

(In main() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

number3 = float(input(“Enter third Float Number: “))

Print Message on the Output Screen and take third Float Number as Input from User 

number1 = 21.2

(In  main() Function)

number2 = 15.3

(In  main() Function)

number3 = 15

(In  main() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

add2(number1, number2, number3)

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

number1 = 21.2

(In  main() Function)

number2 = 15.3

(In  main() Function)

number3 = 15

(In  main() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

Program Control has moved from Calling Function (i.e., main() Function) to the Called Function (i.e., add2() Function). Variable number1, number2 and number3 (i.e., 21.2, 15.3 and 15) has been PASSED as an Argument / Parameter to the Called Function (i.e., add2())

Program Control is with the add2() Function

Function Header – add2()

  • Program Control is with the add2() Function 

·         

    • Function Header 

·         

o    

      • Step 1: Declare three Float Variables number1, number2 and number3 
      • Step 2: Store Values (i.e., 21.2, 15.3 and 15) Passed by Calling Function (i.e., main() Function) in the Variables number1, number2 and number3 (declared in Step 1)

–

–

number1 = 21.2

(In  main() Function)

number2 = 15.3

(In  main() Function)

number3 = 15

(In  main() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

Body of Function – add2()

sum = number1 + number2 + number3

Add all three Float numbers (to calculate their sum) and store result (Output) in a variable (called sum)

  • Calculation

·        sum = number1 + number2 + number3

·        sum = 21.2 + 15.3 + 15.0 = 51.5

·        sum = 51.5

  • Note

·        In the calculation we have used the Variable number1, number2 and number3 (In add2() Function)

number1 = 21.2

(In  main() Function)

number2 = 15.3

(In  main() Function)

number3 = 15

(In  main() Function)

sum = 51.5

(In add2() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

print(number1,”+”,number2,”+”,number3, “=”, sum)

Print three Float numbers (Input) and their sum (Output) on the Output Screen.

number1 = 21.2

(In  main() Function)

number2 = 15.3

(In  main() Function)

number3 = 15

(In  main() Function)

sum = 51.5

(In add2() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

Body of add2() Function is completely executed. Variables declared in the add2() Function will be destroyed and Program Control will move back from the Called Function (add2()) to the Calling Function (main())

Program Control is with the main() Function

–

–

number1 = 21.2

(In  main() Function)

number2 = 15.3

(In  main() Function)

number3 = 15

(In  main() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

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 first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

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 add2();
  • Function Header
    • def add2():   
  • Function Call
    • add2()
  • Analysis 
    • The add2() Function take Arguments / Parameters and does not Return any Value, which is evident from 
  • (number1, number2, number3) 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 Arguments / Parameters and does not Return any Value, in Function Call we use
    • Function Name with (number1, number2, number3)
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 add2(number1, number2, number3);
Function Call
add2(number1, number2, number3);
Function Header 
def add2(number1, number2, number3);
Body of Function 
Contains Code for Input + Processing + Output 
Output of Python Program

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

Execution of Input + Processing + Output
Input + Processing + Output is carried out in the Called Function (i.e., square01() Function)
Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, add2() Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Sum of three Float Numbers on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.
    • Calculating and Displaying Sum of three Float Numbers on the Output Screen was successfully accomplished using add2() Function. Alhamdulillah (الحمدللہ) 

 

  • Comparing Two Variations of Prototype 
Analysis Parameters
No Return Type, No Arguments / Parameters
No Return Type, Arguments / Parameters

Function Prototype

void add01();

void add02(float, float, float)

Function Call

add01();

add02(number01 ,number02, number03)

Function Header 

def add01 ()

def add02(number01 ,number02, number03)

Body of Function 

Contains Code for Input + Processing + Output 

Contains Code for Processing + Output 

Output of Python Program

Enter first float number: 6

Enter second float number: 2.5

Enter third float number: 0.05

Sum of 6 + 2.5 + 0.05 = 8.55

Enter first float number: 6

Enter second float number: 2.5

Enter third float number: 0.05

Sum of 6 + 2.5 + 0.05 = 8.55

Execution of Input + Processing + Output

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

Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., void add02(float, float, float) 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 Sum of three Float Numbers 
  • In Sha Allah, in the next s we will solve the Real-world Task (i.e., Calculate Sum of three Float Numbers) 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 the Sum of Three Float Numbers 
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants them to practice the concept of adding three Float Numbers. For this purpose, she wants to develop a software for her students 😊
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 Sum of three Float Numbers 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
    • Float Number 1 
    • Float Number 2
    • Float Number 3 
  • Output
    • Float Number 1 + Float Number 2 + Float Number 3
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Number1 = 10.25
      • Number2 = 15.30
      • Number3 = 20.55
    • Output
      • 46.1
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Number1 = 2.45
      • Number2 = 6.15
      • Number3 = 9.00
    • Output
      • 17.6
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Number1 = 200.0
      • Number2 = 600.1
      • Number3 = 575.2
    • Output
      • 1375.3
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 three Float Numbers as Input from User, calculates their sum and displays the result (Output) on the Output Screen. 
  • Note – Please implement this using a Function (called add3()). 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 sum of three Float Numbers and display the result on the Output Screen. 
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 1
      • Only take three Float Numbers as Input from User 
    • Requirements 2
      • Display the Sum of Three Float Numbers on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Only take Float Numbers as Input from User
    • Only take Float Number as Input from User (Integers are also allowed)
  • Analysis of Requirement 2 – Display the Sum of three Float Numbers 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
      • Three Float Numbers (called number1, number2 and number3)
    • Instruction(s)
      • Calculate Sum of Three Float Numbers
      • Display Sum of Three Float Numbers on the Output Screen
  • Output
    • number1 +  number2 + number3 
  • Processing
    • Use + Operator to calculate Sum of Three Float Numbers – Add all three Float Numbers
    • Use print() Function  – To display the Sum of Three Float Numbers on the Output Screen
Step 4.2: Write down Function Prototype
  •  Input 
    • Number of Input(s) = 3
      • Number 1
      • Number 2
      • Number 3
    • Data Type of Input(s)
      • Data Type of Number 1 = Float
      • Data Type of Number 2 = Float
      • Data Type of Number 3 = Float
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = Float
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be add3() and Function Prototype should have: Return Type, No Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • int add3();
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 add3() Function, which takes three Float Number as Input from the User, calculate its sum and return the result (Sum of Float Number) to the main() Function. The main() Function will receive the result (Sum of Float Number) returned by the add3() Function and display it on the Output Screen.
Step 4.4: Convert Algorithm (Step 4.3) into Pseudo Code

Function main()

          FUNCTION CALL –  result = add3()

          DISPLAY – result

End Function

Function add3()

         INPUT – number1

         INPUT – number2

         INPUT – number3

         CALCULATE – sum = number1 + number2 + number3 

         DISPLAY – number1, Number 2, number3 and sum

         RETURN – sum

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 Sum of three Float Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 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 sum of three Float Numbers and display the result (Output) 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.      add3()
'''
# Function Prototypes for User Defined Functions 
# int add3(); Function Prototype
def add3():
    try:
        # Input
        # Variable to store Inputs – number1, number2 and number3
        # Variable to store Output - sum
        number1 = float(input("Enter first Float Number: "))
        number2 = float(input("Enter second Float Number: "))
        number3 = float(input("Enter third Float Number: "))
    
        # Processing + Output 
        sum = number1 + number2 + number3
    
        # Display Input and Output
        print(number1,"+",number2,"+",number3, "=", sum)
        # RETRUN result (Output) to the Calling Function
        return sum;
    except ValueError:
        print("Error! Enter Integer Value")    
    
if __name__ == "__main__":  # main() Function 
    # Variable to store Output (returned by the Called Function (i.e.,  add3() Function) - result
    result = add3() # Function Call
        
    # Display Output
    print(result)

				
			
				
					Enter first Float Number: 21.2
Enter second Float Number: 15.3
Enter third Float Number: 15
21.2 + 15.3 + 15.0 = 51.5
				
			
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 = add3()

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., add3(). Note that  add3() is a Function Call.
  • Step 2:  Once Function Call (i.e., add3()) is complete, it will return a Float value on RHS OF THE Assignment Statement i.e., add3() 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 = add3()

·        Step 1: Execute Right Hand Side (RHS) of the Assignment Statement i.e., add3(). Note that add3() is a Function Call.

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

Program Control is with the add3() Function

number1 = float(input(“Enter first Float Number: “))

    

Print Message on the Output Screen and take first Float Number as Input from User 

result =

(In main() Function)

number1 = 21.2

(In add3() Function)

Enter first Float Number: 21.2

number2 = float(input(“Enter second Float Number: “))

    

Print Message on the Output Screen and take second Float Number as Input from User 

result =

(In main() Function)

number1 = 21.2

(In add3() Function)

number2 = 15.3

(In add3() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

number3 = float(input(“Enter third Float Number: “))

Print Message on the Output Screen and take third Float Number as Input from User 

result =

(In main() Function)

number1 = 21.2

(In add3() Function)

number2 = 15.3

(In add3() Function)

number3 = 15

(In add3() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

sum = number1 + number2 + number3

Add all three Float numbers (to calculate their sum) and store result (Output) in a variable (called sum)

  • Calculation

·        sum = number1 + number2 + number3

·        sum = 21.2 + 15.3 + 15.0 = 51.5

·        sum = 51.5

  • Note

·        In the calculation we have used the Variable number1, number2 and number3 (In add3() Function)

result =

(In main() Function)

number1 = 21.2

(In add3() Function)

number2 = 15.3

(In add3() Function)

number3 = 15

(In add3() Function)

sum = 51.5

(In add3() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

print(number1,”+”,number2,”+”,number3, “=”, sum)

Print three Float numbers (Input) and their sum (Output) on the Output Screen.

result =

(In main() Function)

number1 = 21.2

(In add3() Function)

number2 = 15.3

(In add3() Function)

number3 = 15

(In add3() Function)

sum = 51.5

(In add3() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

return sum

This Statement will RETURN the Value stored in Variable sum (i.e., 51.5) to the Calling Function (i.e., main() Function)

result =

(In main() Function)

number1 = 21.2

(In add3() Function)

number2 = 15.3

(In add3() Function)

number3 = 15

(In add3() Function)

sum = 51.5

(In add3() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

Body of add3() Function is completely executed. Variables declared in the add3() Function will be destroyed and Program Control will move back from the Called Function (add3()) to the Calling Function (main())

Program Control is with the main() Function

–

–

result =

(In main() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

result = add3()

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

result = add3()

Store the Float Value (Output) Returned by add3() in Variable result

  • Calculation

·        result = add3()

·        result = 51.5

result = 51.5

(In main() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

print(result)

Print Output (Sum of Float Variables) on the Output Screen.

result = 51.5

(In main() Function)

Enter first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

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 first Float Number: 21.2

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

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 
    • int add3();
  • Function Header
    • def add3():   
  • Function Call
    • add3()
  • Analysis 
    • The add3() 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
int add3();
Function Call
add3();
Function Header 
def add3();
Body of Function 
Contains Code for Input + Processing + Output 
Output of Python Program

Enter second Float Number: 15.3

Enter third Float Number: 15

21.2 + 15.3 + 15.0 = 51.5

Execution of Input + Processing + Output
Input + Processing + Output is carried out in the Called Function (i.e., add3() Function)
Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, add3() Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Sum of three Float Numbers on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.
    • Calculating and Displaying Sum of three Float Numbers on the Output Screen was successfully accomplished using add3() Function. Alhamdulillah (الحمدللہ) 

 

  • Comparing Three Variations of Prototype
Analysis Parameters
No Return Type, No Arguments / Parameters
No Return Type, Arguments / Parameters
Return Type, No Arguments / Parameters
Function Prototypevoid add01();void add02(float, float, float);void add03();
Function Calladd01();add02(number01 ,number02, number03);add03();
Function Header def add01()def add02(number01 ,number02, number03);def add03()
Body of Function Contains Code for Input + Processing + Output Contains Code for Processing + Output Contains Code for Input + Processing + Output 
Return ValueN / AN / A

Use return result to return one Integer Value

return result;

Output of Python Program

Enter first float number: 6

Enter second float number: 2.5

Enter third float number: 0.05

Sum of 6 + 2.5 + 0.05 = 8.55

Enter first float number: 6

Enter second float number: 2.5

Enter third float number: 0.05

Sum of 6 + 2.5 + 0.05 = 8.55

Enter first float number: 6

Enter second float number: 2.5

Enter third float number: 0.05

Sum of 6 + 2.5 + 0.05 = 8.55

Execution of Input + Processing + OutputInput + Processing + Output is carried out in the Called Function (i.e., add01() Function)Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., void add02(float, float, float) Function)Input + Processing + Output is carried out in the Called Function (i.e., add03() 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 Sum of three Float Numbers 
  • In Sha Allah, in the next s we will solve the Real-world Task (i.e., Calculate Sum of three Float Numbers) 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 the Sum of Three Float Numbers 
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants them to practice the concept of adding three Float Numbers. For this purpose, she wants to develop a software for her students 😊
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 Sum of three Float Numbers 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
    • Float Number 1 
    • Float Number 2
    • Float Number 3 
  • Output
    • Float Number 1 + Float Number 2 + Float Number 3
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Number1 = 10.25
      • Number2 = 15.30
      • Number3 = 20.55
    • Output
      • 46.1
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Number1 = 2.45
      • Number2 = 6.15
      • Number3 = 9.00
    • Output
      • 17.6
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Number1 = 200.0
      • Number2 = 600.1
      • Number3 = 575.2
    • Output
      • 1375.3
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 three Float Numbers as Input from User, calculates their sum and displays the result (Output) on the Output Screen.
  • Note – Please implement this using a Function (called add4()). 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 sum of three Float Numbers and display the result on the Output Screen.
Step 3.2: Requirements Gathering
  • The main Requirements of this Software are as follows:
    • Requirement 1
      • Only take three Float Numbers as Input from User 
    • Requirements 2
      • Display the Sum of Three Float Numbers on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Only take Float Numbers as Input from User
    • Only take Float Number as Input from User (Integers are also allowed)
  • Analysis of Requirement 2 – Display the Sum of three Float Numbers 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
      • Three Float Numbers (called number1, number2 and number3)
    • Instruction(s)
      • Calculate Sum of Three Float Numbers
      • Display Sum of Three Float Numbers on the Output Screen
  • Output
    • number1 +  number2 + number3 
  • Processing
    • Use + Operator to calculate Sum of Three Float Numbers – Add all three Float Numbers
    • Use print() Function  – To display the Sum of Three Float Numbers on the Output Screen
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 3
      • Number 1
      • Number 2
      • Number 3
  • Data Type of Input(s)
    • Data Type of Number 1 = Float
    • Data Type of Number 2 = Float
    • Data Type of Number 3 = Float
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = Float
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2), the Name of Function should be add4() and Function Prototype should have: Return Type, Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • int add4();
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 add4() Function and pass three Float Number as Argument / Parameter to it. The add4() Function, will receive the Argument / Parameter passed to it, calculate its sum (called sum). The ad04() Function will return the result to the main() Function. The main() Function will receive the result returned by the add4() Function and display it on the Output Screen.
Step 4.4: Convert Algorithm (Step 4.3) into Pseudo Code

Function main()

         INPUT – number1

         INPUT – number2

         INPUT – number3

         FUNCTION CALL – result = add4(number1, number2, number3)

          DISPLAY – result

End Function

Function add4(number1, number2, number3)

         CALCULATE – sum = number1 + number2 + number3

         RETURN result

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 Sum of three Float Numbers
__program__version__      = 1.1
__programing__language__  = python 3.8.3
__operating__system__     = Ubuntu 18.04.1 LTS 64 bit 
                            Operating System
__IDE __                  = jupyter__notebook 5.5.0
___Start__Date__          = 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 sum of three Float Numbers and display the result (Output) on the Output Screen. Please note that the Function Prototype will be: No Return Type, Arguments / Parameters 😊

This program comprises of two Functions.
1.	main()
2.	add4()
'''

# Function Prototypes for User Defined Functions 
# int add4(number1, number2, number3); Function Prototype

def add4(number1, number2, number3):  
    # Processing + Output 
    sum = number1 + number2 + number3
    
    # RETRUN Value (Output) to the Calling Function
    return sum;
 
if __name__ == "__main__":  # main() Function 
    try:
        # Input
        # Variable to store Inputs – number1, number2 and number3
        # Variable to store Output - sum
        number1 = float(input("Enter first Float Number: "))
        number2 = float(input("Enter second Float Number: "))
        number3 = float(input("Enter third Float Number: "))

        result = add4(number1, number2, number3) # Function Call
        # Display Input and Output
        print(number1,"+",number2,"+",number3, "=", result)
    except ValueError:
        print("Error! Enter Integer Value")   

				
			
				
					Enter first Float Number: 21.2
Enter second Float Number: 15.3
Enter third Float Number: 15
21.2 + 15.3 + 15.0 = 51.5
				
			
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
number1 = float(input(“Enter first Float Number: “))      Print Message on the Output Screen and take first Float Number as Input from User  number1 = 21.2 (In main() Function) Enter first Float Number: 21.2
number2 = float(input(“Enter second Float Number: “))      Print Message on the Output Screen and take second Float Number as Input from User  number1 = 21.2 (In main() Function) number2 = 15.3 (In main() Function) Enter first Float Number: 21.2 Enter second Float Number: 15.3
number3 = float(input(“Enter third Float Number: “)) Print Message on the Output Screen and take third Float Number as Input from User  number1 = 21.2 (In  main() Function) number2 = 15.3 (In  main() Function) number3 = 15 (In  main() Function) Enter first Float Number: 21.2 Enter second Float Number: 15.3 Enter third Float Number: 15
result = add4(number1, number2, number3) 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., add4(). Note that  add4() is a Function Call.
  • Step 2:  Once Function Call (i.e., add4()) is complete, it will return a Float value on RHS OF THE Assignment Statement i.e., add4() 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
number1 = 21.2 (In  main() Function) number2 = 15.3 (In  main() Function) number3 = 15 (In  main() Function) result =  (In  main() Function) Enter first Float Number: 21.2 Enter second Float Number: 15.3 Enter third Float Number: 15
  • result = add4()
·        Step 1:  Execute Right Hand Side (RHS) of the Assignment Statement i.e., add4(). Note that add4() is a Function Call. ·        This Python Statement is a Function Call. Therefore, Program Control will move from Calling Function (main()) to the Called Function (add4()).
Program Control is with the add4() Function
Function Header – add4()
  • Program Control is with the add4() Function 
    • Function Header 
      • Step 1: Declare three Float Variables number1, number2 and number3 
      • Step 2: Store Values (i.e., 21.2, 15.3 and 15) Passed by Calling Function (i.e., main() Function) in the Variables number1, number2 and number3 (declared in Step 1)
– – number1 = 21.2 (In  main() Function) number2 = 15.3 (In  main() Function) number3 = 15 (In  main() Function) result =  (In  main() Function) Enter first Float Number: 21.2 Enter second Float Number: 15.3 Enter third Float Number: 15
Body of Function – add4()
sum = number1 + number2 + number3 Add all three Float numbers (to calculate their sum) and store result (Output) in a variable (called sum)
  • Calculation
·        sum = number1 + number2 + number3 ·        sum = 21.2 + 15.3 + 15.0 = 51.5 ·        sum = 51.5
  • Note
·        In the calculation we have used the Variable number1, number2 and number3 (In add4() Function)
number1 = 21.2 (In  main() Function) number2 = 15.3 (In  main() Function) number3 = 15 (In  main() Function) sum = 51.5 (In add4() Function) result =  (In  main() Function) Enter first Float Number: 21.2 Enter second Float Number: 15.3 Enter third Float Number: 15
return sum This Statement will RETURN the Value stored in Variable sum (i.e., 51.5) to the Calling Function (i.e., main() Function) number1 = 21.2 (In  main() Function) number2 = 15.3 (In  main() Function) number3 = 15 (In  main() Function) sum = 51.5 (In add4() Function) result =  (In  main() Function) Enter first Float Number: 21.2 Enter second Float Number: 15.3 Enter third Float Number: 15 21.2 + 15.3 + 15.0 = 51.5
Body of add4() Function is completely executed. Variables declared in the add4() Function will be destroyed and Program Control will move back from the Called Function (add4()) to the Calling Function (main())
Program Control is with the main() Function
– – number1 = 21.2 (In  main() Function) number2 = 15.3 (In  main() Function) number3 = 15 (In  main() Function) result =  (In  main() Function) Enter first Float Number: 21.2 Enter second Float Number: 15.3 Enter third Float Number: 15 21.2 + 15d.3 + 15.0 = 51.5
result = add4()
  • Step 1:  Once Function Call (i.e., add4()) is complete, it will return a Float value on RHS of the Assignment Statement i.e., add4() will be replaced with the Returned Value
  • result = 51.5
  • Step 2:  Store the Returned Value (Step 2) in the Variable result on the Left Hand Side (LHS) of the Assignment Statement
result = add4(number1, number2, number3) Store the Float Value (Output) Returned by add4() in Variable result
  • Calculation
·        result = add4() ·        result = 51.5
number1 = 21.2 (In  main() Function) number2 = 15.3 (In  main() Function) number3 = 15 (In  main() Function) result = 51.5 (In  main() Function) Enter first Float Number: 21.2 Enter second Float Number: 15.3 Enter third Float Number: 15 21.2 + 15d.3 + 15.0 = 51.5
print(number1,”+”,number2,”+”,number3, “=”, sum) Print number (Input) on the Output Screen. Note that we are displaying number (Input) in the add4() Function because Input was taken from user in add4() and main() Function does not know about Input number1 = 21.2 (In  main() Function) number2 = 15.3 (In  main() Function) number3 = 15 (In  main() Function) result = 51.5 (In  main() Function) Enter first Float Number: 21.2 Enter second Float Number: 15.3 Enter third Float Number: 15 21.2 + 15d.3 + 15.0 = 51.5
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 first Float Number: 21.2 Enter second Float Number: 15.3 Enter third Float Number: 15 21.2 + 15.3 + 15.0 = 51.5
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 
      • int add4();
    • Function Header
      • def add4():   
    • Function Call
      • add4()
  • Analysis 
    • The add4(number1, number2, number3) 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 (number1, number2, number3)
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
int add4(number1, number2, number3);
Function Call
add4(number1, number2, number3);
Function Header
def add4(number1, number2, number3)
Body of Function
Contains Code for Input + Processing + Output 
Output of Python Program
Enter second Float Number: 15.3 Enter third Float Number: 15 21.2 + 15.3 + 15.0 = 51.5
Execution of Input + Processing + Output
Input + Processing + Output is carried out in the Called Function (i.e., square01() Function)
Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, add4() Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Sum of three Float Numbers on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.
    • Calculating and Displaying Sum of three Float Numbers on the Output Screen was successfully accomplished using add4() Function. Alhamdulillah (الحمدللہ) 
  • Comparing Four Variations of Prototype 
Analysis Parameters
No Return Type, No Arguments / Parameters
No Return Type, Arguments / Parameters
Return Type, No Arguments / Parameters
Return Type, Arguments / Parameters

Function Prototype

void add01();

void add02(float, float, float);

void add03();

void add04(float, float, float);

Function Call

add01();

add02(number01 ,number02, number03);

add03();

add04 (number01 ,number02, number03);

Function Header 

def add01()

def add02(number01 ,number02, number03);

def add03()

void add04 (number01 ,number02, number03);

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

Return Value

Use return result to return one Integer Value

return result;

Output of Python Program

Enter first float number: 6

Enter second float number: 2.5

Enter third float number: 0.05

Sum of 6 + 2.5 + 0.05 = 8.55

Enter first float number: 6

Enter second float number: 2.5

Enter third float number: 0.05

Sum of 6 + 2.5 + 0.05 = 8.55

Enter first float number: 6

Enter second float number: 2.5

Enter third float number: 0.05

Sum of 6 + 2.5 + 0.05 = 8.55

Enter first float number: 6

Enter second float number: 2.5

Enter third float number: 0.05

Sum of 6 + 2.5 + 0.05 = 8.55

Execution of Input + Processing + Output

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

Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., void add02(float, float, float) Function)

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

Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., void add04(float, float, float) 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 radius_of_circle()) which takes a circumference (Float Number) as Input from User and prints its radius of Circle on the Output Screen.

·        r = C / 2PI

Required Output

Enter Circumference of Circle: 20.0

Radius of Circle =20 / 2*3.14 = 3.18

 

                        Task 2

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called area_of_sphere()) which takes radius (Float Number) as Input and prints surface area of sphere on the Output Screen.
    • surface area of sphere =  4πr2

Required Output

Enter Radius of Sphere: 9.45

Surface Area of Sphere = 4 * 3.14 * 9.45 = 118.7

 

                        Task 3

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called volume_of_cube()) which takes edge length (Float Number i.e., a) as Input and prints volume of cube on the Output Screen.

·        Volume of Cube =  a3

Required Output   

Enter Edge Length of Cube: 1.65

Volume of Cube = 4.49

 

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 26 - A Step by Step Example to Write Functions II
  • Previous
Chapter 28 – Basics of Data Structures
  • Next
Share this article!
Facebook
Twitter
LinkedIn
About Us

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

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

© 2022 Ilmo Irfan. All Rights Reserved.