Chapter 25 - A Step by Step Example to Write Functions I
- 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 Square of an Integer Number
- In Sha Allah, in the next Slides we will solve the Real-world Task (i.e., Calculate Square of an Integer Number) using Functions by following the Steps mentioned above
- Note
- The Function Prototype will be
- No Return Type, No Arguments / Parameters
- The Function Prototype will be
- Example 1 - Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the 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() FUNCTION CALL – square01() End Function Function square01() INPUT – number CALCULATE – result = number * number DISPLAY – 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 Square of an Integer Number
__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 square of an Integer Number and display the result on the Output Screen. Please note that the Function Prototype will be: No Return Type, No Arguments / Parameters 😊
This program comprises of four Functions.
main()
square01()
'''
# Function Prototypes for User Defined Functions
# void square01(); Function Prototype
def square01():
try:
# Input
# Variable to store Input - number
# Variable to store Output - result
number = int(input("Enter an Integer Number: "))
# Processing + Output
result = number * number
# Display Input and Output
print("Square of", number, "=", result)
except ValueError:
print("Error! Enter Integer Value")
if __name__ == "__main__": # main() Function
square01() # Function Call
Enter an Integer Number: 6
Square of 6 = 36
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 within the main() Function 😊 | |||||
Program Control is with the main() Function | |||||
square01() | This Python Statement is a Function Call. Therefore, Program Control will move from Calling Function (main()) to the Called Function (square01()). | – | – | ||
Program Control is with the square01() Function | |||||
number = int(input(“Enter an Integer Number: “)) | Print Message on the Output Screen and take an Integer Number as Input from User | number = 6 (In square01() Function) | Enter an Integer Number: 6 | ||
result = number * number | Multiply number with itself (to calculate its square) and store result in a variable (called result)
· result = number * number · result = 6 * 6 · result = 36
· In the calculation we have used the Variable number (In square01() Function) | number = 6 (In square01() Function) result = 36 (In square01() Function) | Enter an Integer Number: 6 | ||
print(“Square of”, number, “=”, result) | Print number (Input) and its square (Output) on the Output Screen. | number = 6 (In square01() Function) result = 36 (In square01() Function) | Enter an Integer Number: 6 Square of 6 = 36 | ||
Body of square01() Function is completely executed. Variables declared in the square01() Function will be destroyed and Program Control will move back from the Called Function (square01()) to the Calling Function (main()) | |||||
Program Control is with the main() Function | |||||
– | – | – | Enter an Integer Number: 6 Square of 6 = 36 | ||
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 an Integer Number: 6 Square of 6 = 36 | ||
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
| ||||||||||||||
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, No Arguments / Parameters) | ||||||||||||||
| ||||||||||||||
Step 7.3: Main Findings | ||||||||||||||
| ||||||||||||||
Step 7.4: Conclusion | ||||||||||||||
· Calculating and Displaying Square of an Integer Number on the Output Screen was successfully accomplished using square01() 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 Square of an Integer Number
- In Sha Allah, in the next Slides we will solve the Real-world Task (i.e., Calculate Square of an Integer Number) using Functions by following the Steps mentioned above
- Note
- The Function Prototype will be
- No Return Type, Arguments / Parameters
- The Function Prototype will be
- Example 2 - Steps (Solving Programming Tasks using Functions)
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 |
Pseudo Code Function main() INPUT – number FUNCTION CALL – square02(number) End Function Function square02(number) CALCULATE – result = number * number DISPLAY – 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 Square of an Integer Number
__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 square of an Integer Number and display the result on the Output Screen. Please note that the Function Prototype will be: No Return Type, Arguments / Parameters 😊
This program comprises of four Functions.
3. main()
4. square02()
'''
# Function Prototypes for User Defined Functions
# void square02(number); Function Prototype
def square02(number):
# Variable to store Input – number
# Variable to store Output - result
# Processing + Output
result = number * number
# Display Input and Output
print("Square of", number, "=", result)
if __name__ == "__main__": # main() Function
try:
# Input
# Variable to store Input - number
number = int(input("Enter an Integer Number: "))
square02(number) # Function Call
except ValueError:
print("Error! Enter Integer Value")
Enter an Integer Number: 6
Square of 6 = 36
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 within the main() Function 😊. | |||
Program Control is with the main() Function | |||
number = int(input(“Enter an Integer Number: “)) | Print Message on the Output Screen and take an Integer Number as Input from User | number = 6 (In main() Function) | Enter an Integer Number: 6 |
square02(number) | This Python Statement is a Function Call.
| number = 6 (In main() Function) | Enter an Integer Number: 6 |
Program Control has moved from Calling Function (i.e., main() Function) to the Called Function (i.e., square02() Function). Variable number (i.e., 6) has been PASSED as an Argument / Parameter to the Called Function (i.e., square02()) | |||
Program Control is with the square02() Function | |||
Function Header – square02() | |||
| |||
number = 6 (In main() Function) number = 6 (In square02() Function) | Enter an Integer Number: 6 | ||
Body of Function – square02() | |||
result = number * number | Multiply number with itself (to calculate its square) and store result in a variable (called result)
· result = number * number · result = 6 * 6 · result = 36
· In the calculation we have used the Variable number (In square02() Function) | number = 6 (In main() Function) number = 6 (In square02() Function) result = 36 (In square02() Function) | Enter an Integer Number: 6 |
print(“Square of”, number, “=”, result) | Print number (Input) and its square (Output) on the Output Screen. | number = 6 (In main() Function) number = 6 (In square02() Function) result = 36 (In square02() Function) | Enter an Integer Number: 6 Square of 6 = 36 |
Body of square02() Function is completely executed. Variables declared in the square02() Function will be destroyed and Program Control will move back from the Called Function (square02()) to the Calling Function (main()) | |||
Program Control is with the main() Function | |||
– | – | number = 6 (In main() Function) | Enter an Integer Number: 6 Square of 6 = 36 |
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 Therefore, Python Program will be Terminated 😊 | |||
– | – | – | Enter an Integer Number: 6 Square of 6 = 36 |
Step 7: Analysis, Main Findings and Conclusion | ||||||||||||||
Step 7.1: Analysis of Python Program – Calling a Function (No Return Type, Arguments / Parameters) | ||||||||||||||
Note the Function Prototype, Function Header and Function Call
| ||||||||||||||
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, 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 square01(); | void square02(int); |
Function Call | square01(); | square02(number); |
Function Header | def square01() | def square02(number) |
Body of Function | Contains Code for Input + Processing + Output | Contains Code for Processing + Output |
Output of Python Program | Enter an Integer Number: 6 Square of 6 * 6 = 36 | Enter an Integer Number: 6 Square of 6 * 6 = 36 |
Execution of Input + Processing + Output | Input + Processing + Output is carried out in the Called Function (i.e., square01() Function) | Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., square02() 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 Square of an Integer Number
- In Sha Allah, in the next Slides we will solve the Real-world Task (i.e., Calculate Square of an Integer Number) using Functions by following the Steps mentioned above
- Note
- The Function Prototype will be
- Return Type, No Arguments / Parameters
- The Function Prototype will be
- Example 3 - Steps (Solving Programming Tasks using Functions)
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 |
Pseudo Code Function main() FUNCTION CALL – result = square03() DISPLAY – result End Function Function square03() INPUT – number CALCULATE – result = number * number Display – number 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 Square of an Integer Number
__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 square of an Integer Number and display the result on the Output Screen. Please note that the Function Prototype will be: Return Type, No Arguments / Parameters 😊
This program comprises of four Functions.
5. main()
6. square03()
'''
# Function Prototypes for User Defined Functions
# int square03(); Function Prototype
def square03():
try:
# Input
# Variable to store Input - number
# Variable to store Output - result
number = int(input("Enter an Integer Number: "))
# Processing + Output
result = number * number;
# Display Output
print("Square of",number,"=")
# RETRUN result (Output) to the Calling Function
return result;
except ValueError:
print("Error! Enter Integer Value")
if __name__ == "__main__": # main() Function
# Variable to store Output (returned by the Called Function (i.e., square03() Function) - result
result = square03() # Function Call
# Display Output
print(result)
Enter an Integer Number: 3
Square of 3 =
9
Step 6: Dry Run the Python Program | ||||
Python Statement | Processing | Memory | Output Screen | |
Sart (Program Execution) – Execution of Python Program starts from the main() Function. Therefore, Program Control will be with the main() Function 😊. | ||||
Program Control is with the main() Function | ||||
result = square03() | 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., square03(). Note that square03() is a Function Call. · This Python Statement is a Function Call. Therefore, Program Control will move from Calling Function (main()) to the Called Function (square03()). | ||||
Program Control is with the square03() Function | ||||
number = int(input(“Enter an Integer Number: “)) | Print Message on the Output Screen and take an Integer Number as Input from User | result (In main() Function) number = 6 (In square03() Function) | Enter an Integer Number: 6 | |
result = number * number | Multiply number with itself (to calculate its square) and store result in a variable (called result)
· result = number * number · result = 6 * 6 · result = 36
· In the calculation we have used the Variable number (In square03() Function) | result (In main() Function) number = 6 (In square03() Function) result = 36 (In square03() Function) | Enter an Integer Number: 6 | |
print(“Square of”, number, “=”) | Print number (Input) on the Output Screen. Note that we are displaying number (Input) in the square03() Function because Input was taken from user in square03() and main() Function does not know about Input | result (In main() Function) number = 6 (In square03() Function) result = 36 (In square03() Function) | Enter an Integer Number: 6 Square of 6 = | |
return result | This Statement will RETURN the Value stored in Variable result (i.e., 36) to the Calling Function (i.e., main() Function) | result (In main() Function) number = 6 (In square03() Function) result = 36 (In square03() Function) | Enter an Integer Number: 6 Square of 6 = | |
Body of square03() Function is completely executed. Variables declared in the square03() Function will be destroyed and Program Control will move back from the Called Function (square03()) to the Calling Function (main()) | ||||
Program Control is with the main() Function | ||||
result (In main() Function) | Enter an Integer Number: 6 Square of 6 = | |||
result = square03()
| ||||
result = square03() | Store the Integer Value (Output) Returned by square03() in Variable result
| result = 36 (In main() Function) | Enter an Integer Number: 6 Square of 6 = 36 | |
print(result) | Print Output (Square of Integer Number) on the Output Screen. | result = 36 (In main() Function) | Enter an Integer Number: 6 Square of 6 = 36 | |
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 Therefore, Python Program will be Terminated 😊 | ||||
– | – | – | Enter an Integer Number: 6 Square of 6 = 36 |
Step 7: Analysis, Main Findings and Conclusion | ||||||||||||||
Step 7.1: Analysis of Python Program – Calling a Function (Return Type, No Arguments / Parameters) | ||||||||||||||
Note the Function Prototype, Function Header and Function Call
| ||||||||||||||
Step 7.2: Summary of Analysis – Calling a Function (Return Type and 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 square01(); | void square02(int); | void square03(); |
Function Call | square01(); | square02(number); | square03(); |
Function Header | def square01() | def square02(number) | def square03() |
Body of Function | Contains Code for Input + Processing + Output | Contains Code for Processing + Output | Contains Code for Input + Processing + Output |
Output of Python Program | Enter an Integer Number: 6 Square of 6 * 6 = 36 | Enter an Integer Number: 6 Square of 6 * 6 = 36 | Use return result to return one Integer Value return result; |
Execution of Input + Processing + Output | Input + Processing + Output is carried out in the Called Function (i.e., square01() Function) | Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., square02() Function) | Enter an Integer Number: 6 Square of 6 * 6 = 36 |
Analysis Parameters | No Return Type, No Arguments / Parameters | No Return Type, Arguments / Parameters | Input + Processing + Output is carried out in the Called Function (i.e., square03() 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 Square of an Integer Number
- In Sha Allah, in the next Slides we will solve the Real-world Task (i.e., Calculate Square of an Integer Number) 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)
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 |
Pseudo Code Function main() INPUT – number FUNCTION CALL – result = square04(number) DISPLAY – result End Function Function square04(number) CALCULATE – result = number * number 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 Square of an Integer Number
__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 square of an Integer Number and display the result on the Output Screen. Please note that the Function Prototype will be: Return Type, Arguments / Parameters 😊 This program comprises of four Functions.
7. main()
8. square04()
'''
# Function Prototypes for User Defined Functions
# int square04(number); Function Prototype
def square04(number):
# Processing + Output
result = number * number;
# RETRUN Value (Output) to the Calling Function
return result;
if __name__ == "__main__": # main() Function
try:
# Input
# Variable to store Input - number
# Variable to store the Value (Output) RETURNED by Called Function - result
number = int(input("Enter an Integer Number: "))
result = square04(number) # Function Call
# Display Input and Output
print("Square of", number,"=",result)
except ValueError:
print("Error! Enter Integer Value")
Enter an Integer Number: 6
Square of 6 = 36
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 within the main() Function 😊. | |||
Program Control is with the main() Function | |||
number = int(input(“Enter an Integer Number: “)) | Print Message on the Output Screen and take an Integer Number as Input from User | number = 6 (In main() Function) | Enter an Integer Number: 6 |
result = square04(number) | This Python Statement is an Assignment Statement and will be executed in the following Three Steps.
| number = 6 (In main() Function) result (In main() Function) | Enter an Integer Number: 6 |
· Step 1: Execute Right Hand Side (RHS) of the Assignment Statement i.e., square04(). Note that square04() is a Function Call. · This Python Statement is a Function Call. Therefore, Program Control will move from Calling Function (main()) to the Called Function (square04()). | |||
Program Control is with the square04() Function | |||
Function Header – square04() Function | |||
| |||
– | – | number = 6 (In main() Function) result (In main() Function) number = 6 (In square04() Function) | Enter an Integer Number: 6 |
Body of Function – square04() Function | |||
result = number * number | Multiply number with itself (to calculate its square) and store result in a variable (called result)
· result = number * number · result = 6 * 6 · result = 36
· In the calculation we have used the Variable number (In square04() Function) | number = 6 (In main() Function) result (In main() Function) number = 6 (In square04() Function) result = 36 (In square04() Function) | Enter an Integer Number: 6 |
return result | This Statement will RETURN the Value stored in Variable result (i.e., 36) to the Calling Function (i.e., main() Function) | number = 6 (In main() Function) result (In main() Function) number = 6 (In square04() Function) result = 36 (In square04() Function) | Enter an Integer Number: 6 |
Body of square04() Function is completely executed. Variables declared in the square04() Function will be destroyed and Program Control will move back from the Called Function (square04()) to the Calling Function (main()) | |||
Program Control is with the main() Function | |||
number = 6 (In main() Function) result (In main() Function) | Enter an Integer Number: 6 | ||
result = square04()
| |||
result = square04(number) | Store the Integer Value (Output) Returned by square04() in Variable result
· result = square04() · result = 36 | number = 6 (In main() Function) result = 36 (In main() Function) | Enter an Integer Number: 6 |
print(“Square of”, number,”*”,number,”=”, result) | Print number (Input) on the Output Screen. Note that we are displaying number (Input) in the square04() Function because Input was taken from user in square04() and main() Function does not know about Input | number = 6 (In main() Function) result = 36 (In main() Function) | Enter an Integer Number: 6 Square of 6 = 36 |
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 Therefore, Python Program will be Terminated 😊 | |||
– | – | – | Enter an Integer Number: 6 Square of 6 = 36 |
Step 7: Analysis, Main Findings and Conclusion | ||||||||||||||
Step 7.1: Analysis of Python Program – Calling a Function (Return Type, Arguments / Parameters) | ||||||||||||||
Note the Function Prototype, Function Header and Function Call
| ||||||||||||||
Step 7.2: Summary of Analysis – Calling a Function (Return Type and 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 | No Return Type, Arguments / Parameters |
Function Prototype | void square01(); | void square02(int); | void square03(); | void square04(int); |
Function Call | square01(); | square02(number); | square03(); | square04(number); |
Function Header | def square01() | def square02(number) | def square03() | def square04(number) |
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 |
Output of Python Program | Enter an Integer Number: 6 Square of 6 * 6 = 36 | Enter an Integer Number: 6 Square of 6 * 6 = 36 | Use return result to return one Integer Value return result; | Use return result to return one Integer Value return result; |
Execution of Input + Processing + Output | Input + Processing + Output is carried out in the Called Function (i.e., square01() Function) | Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., square02() Function) | Enter an Integer Number: 6 Square of 6 * 6 = 36 | Enter an Integer Number: 6 Square of 6 * 6 = 36 |
Analysis Parameters | No Return Type, No Arguments / Parameters | No Return Type, Arguments / Parameters | Input + Processing + Output is carried out in the Called Function (i.e., square03() Function) | Input was taken in the Calling Function (i.e., main() Function) and Processing + Output is carried out in the Called Function (i.e., square04(number) 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 01 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Enter an Integer Number: 2 Cube of 2 = 8 |
Task 02 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Enter your Current Savings of wealth: 50000 Zakat = 2.5% of 50000 Zakat = 1250 |
Task 03 |
Completely and Correctly Understand the Task |
Task Description |
|
Required Output |
Enter amount of Sadqa: 200 Allah will give you 10 times of 200 = 2000 |
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: