Chapter 27 - A Step by Step Example to Write Functions III
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
- Supporting Material
Solving Programming Tasks using Functions
- Steps – Solving Programming Tasks using Functions
- In Sha Allah, we will follow the following Steps to solve the Programming Tasks using Functions
- Step 1: Completely and Correctly Understand the 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.1.1: Write Down Input and Output of the Real-world Task
- Step 2.2: Convert the Real-world Task Description (Step 1.2) into Programming Task Description
- Step 2.1: Check Whether the Real-world Task can be treated as a Programming Task?
- Step 3: Completely and Correctly Understand the Programming Task Description
- Step 3.1: Completely and Correctly Understand the Programming Task Description
- Step 3.2: Requirements Gathering
- Step 3.3: Requirements Analysis
- Step 4: Plan and Design Solution to the Programming Task
- Step 4.1: Identify 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
- Step 1: Completely and Correctly Understand the Real-world Task
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 |
|
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 |
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)
· sum = number1 + number2 + number3 · sum = 21.2 + 15.3 + 15.0 = 51.5 · sum = 51.5
· 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) | ||||||||||||||
| ||||||||||||||
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters) | ||||||||||||||
| ||||||||||||||
Step 7.3: Main Findings | ||||||||||||||
| ||||||||||||||
Step 7.4: Conclusion | ||||||||||||||
|
Example 2 - Solving Programming Tasks using Functions (with Function Prototype: No Return Type, Arguments / Parameters)
- Example - Steps (Solving Programming Tasks using Functions)
- 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 |
|
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 |
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() | |||
·
· o
| |||
– | – | 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)
· sum = number1 + number2 + number3 · sum = 21.2 + 15.3 + 15.0 = 51.5 · sum = 51.5
· 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) | ||||||||||||||
| ||||||||||||||
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters) | ||||||||||||||
| ||||||||||||||
Step 7.3: Main Findings | ||||||||||||||
| ||||||||||||||
Step 7.4: Conclusion | ||||||||||||||
|
- Comparing Two Variations of Prototype
Analysis Parameters | No Return Type, No Arguments / Parameters | No Return Type, Arguments / Parameters |
Function Prototype | void 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 |
|
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 |
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.
| result = (In main() Function) | – |
· 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)
· sum = number1 + number2 + number3 · sum = 21.2 + 15.3 + 15.0 = 51.5 · sum = 51.5
· 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()
| |||
result = add3() | Store the Float Value (Output) Returned by add3() in Variable result
· 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) | ||||||||||||||
| ||||||||||||||
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters) | ||||||||||||||
| ||||||||||||||
Step 7.3: Main Findings | ||||||||||||||
| ||||||||||||||
Step 7.4: Conclusion | ||||||||||||||
|
- Comparing Three Variations of Prototype
Analysis Parameters | No Return Type, No Arguments / Parameters | No Return Type, Arguments / Parameters | Return Type, No Arguments / Parameters |
Function Prototype | void add01(); | void add02(float, float, float); | void add03(); |
Function Call | add01(); | 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 Value | N / A | N / 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 + 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) |
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
- The Function Prototype will be
- Example 4 - Steps (Solving Programming Tasks using Functions)
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 |
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.
|
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 |
|
|||
Program Control is with the add4() Function | |||
Function Header – add4() | |||
|
|||
– | – | 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)
|
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()
|
|||
result = add4(number1, number2, number3) | Store the Float Value (Output) Returned by add4() in Variable result
|
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) |
||||||||||||||
|
||||||||||||||
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters) |
||||||||||||||
|
||||||||||||||
Step 7.3: Main Findings |
||||||||||||||
|
||||||||||||||
Step 7.4: Conclusion |
||||||||||||||
|
- Comparing Four Variations of Prototype
Analysis Parameters | No Return Type, No Arguments / Parameters | No Return Type, Arguments / Parameters | 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 Task 1
- Task
- Consider the following Tasks and answer the questions given below
- Note
- Your answer should be
- Well Justified
- Your answer should be
- Questions
- Write Function in Python Programs for all the Tasks given below by following the Steps given in this Lecture considering the following variation of Function Prototype i.e.,
- Solving Programming Tasks using Functions with Function Prototype:
- No Return Type, No Arguments / Parameters
- No Return Type, Arguments / Parameters
- Return Type, No Arguments / Parameters
- Return Type, Arguments / Parameters
- Solving Programming Tasks using Functions with Function Prototype:
- Write Function in Python Programs for all the Tasks given below by following the Steps given in this Lecture considering the following variation of Function Prototype i.e.,
Task 1 |
Completely and Correctly Understand the Task |
Task Description |
· 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 |
|
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 |
· Volume of Cube = a3 |
Required Output |
Enter Edge Length of Cube: 1.65 Volume of Cube = 4.49 |
Your Turn Task 1
- Task
- Write at least 6 different Tasks which are very similar to the ones given in the TODO Tasks
- For each Task write a Python Program using the following Steps mentioned in this Lecture considering the following variation of Function Prototype i.e.,
- Solving Programming Tasks using Functions with Function Prototype:
- No Return Type, No Arguments / Parameters
- No Return Type, Arguments / Parameters
- Return Type, No Arguments / Parameters
- Return Type, Arguments / Parameters
- Solving Programming Tasks using Functions with Function Prototype: