Skip to content

Ilm o Irfan

Technologies

  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Menu
  • Home
  • Courses
    • Free Courses
  • Motivational Seminars
  • Blog
  • Books
  • Contact
Search
Close this search box.
Explore Courses

Introduction to Python

  • September 28, 2022
  • No Comments
  • Home
  • Free Book

Table of Contents

Chapter 25 - A Step by Step Example to Write Functions I

  • 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 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
  • Example 1 - Steps (Solving Programming Tasks using Functions)
Completely and Correctly Understand the Real-world Task
Step 1.1: Real-world Task
  • Calculate the Square of an Integer Number 
Step 1.2: Real-world Task Description
  • Ms. Samavi is teaching Math to Grade II students. She wants them to practice the concept of squaring an Integer Number. 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 Square of an Integer Number 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
    • An Integer Number
  • Output
    • Square of the given Integer Number
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Number = 10
    • Output
      • 100
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Number = 7
    • Output
      • 49
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Number = 5
    • Output
      • 25
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 an Integer Number as Input from User, calculates its square and displays the result on the Output Screen. 
  • Note – Please implement this using a Function (called square01()). 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 Square of an Integer Number 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 Integer Number as Input from User
    • Requirement 2
      • Display the Square of Integer Number on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Only take Integer Number as Input from User
    • Only take Integer Number as Input from User (Floats are not allowed)
  • Analysis of Requirement 2 – Display the Square of an Integer Number 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
      • An Integer Number (called number)
    • Instruction(s)
      • Calculate Square of Integer Number
      • Display Square of Integer Number on the Output Screen
  • Output
    • number * number 
  • Processing
    • Use * Operator to calculate Square of Integer Number – Multiply Integer Number with itself
    • Use print() Function  – To display the Square of Integer Number on the Output Screen
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 1
    • Data Type of Input = Integer
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = Integer
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be square01() and Function Prototype should have: No Return Type, No Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • void square01();
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 square01() Function, which will take an Integer Number as Input from User, calculate its square and display the result on the Output Screen.
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)

  • Calculation

·        result = number * number

·        result = 6 * 6

·        result = 36

  • Note

·        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 

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

Enter an Integer Number: 6

Square of 6 = 36 

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 (الحمدللہ) 😊, square01() Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Square of an Integer Number on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.

·        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
  • Example 2 - Steps (Solving Programming Tasks using Functions)
Step 1: Completely and Correctly Understand the Real-world Task
Step 1.1: Real-world Task
  • Calculate the Square of an Integer Number 
Step 1.2: Real-world Task Description
  • Ms. Samavi is teaching Math to Grade II students. She wants them to practice the concept of squaring an Integer Number. 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 Square of an Integer Number 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
    • An Integer Number
  • Output
    • Square of the given Integer Number
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Number = 10
    • Output
      • 100
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Number = 7
    • Output
      • 49
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Number = 5
    • Output
      • 25
Step 2.1.2: Conclusion
  • Yes, we MAY Treat Real-world Task as a Programming Task
Step 2.2: Convert the Real-world Task Description (Step 1.2) into Programming Task Description
  • Write a Python Program takes an Integer Number as Input from User, calculates its square and displays the result on the Output Screen. 
    • Note – Please implement this using a Function (called square02(number)). 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 Square of an Integer Number 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 Integer Number as Input from User
    • Requirement 2
      • Display the Square of Integer Number on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Only take Integer Number as Input from User
    • Only take Integer Number as Input from User (Floats are not allowed)
  • Analysis of Requirement 2 – Display the Square of an Integer Number 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
      • An Integer Number (called number)
    • Instruction(s)
      • Calculate Square of Integer Number
      • Display Square of Integer Number on the Output Screen
  • Output
    • number * number 
  • Processing
    • Use * Operator to calculate Square of Integer Number – Multiply Integer Number with itself
    • Use print() Function  – To display the Square of Integer Number on the Output Screen
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 1
    • Data Type of Input = Integer
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = Integer
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be square02(number) and Function Prototype should have: No Return Type, Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • void square02(number);
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 square02() Function and pass Integer Number as Argument / Parameter to it. The square02() Function, will receive the Argument / Parameter passed to it, calculate its square and display the result on the Output Screen.
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. 

  • The main() Function will pass Variable number as an Argument / Parameter to the square02() Function.  
  • Program Control will move from Calling Function (main()) to the Called Function (square02()).

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()
  • Program Control is with the square02() Function 
    • Function Header 
      • Step 1: Declare a Variable number 
      • Step 2: Store Value (i.e., 6) Passed by Calling Function (i.e., main() Function) in the Variable number (declared in Step 1)
  

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)

  • Calculation

·        result = number * number

·        result = 6 * 6

·        result = 36

  • Note

·        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 

  • Function Prototype 
    • void square02(number);
  • Function Header
    • def square02(number):   
  • Function Call
    • square02()
  • Analysis 
    • The square02(number) Function takes an Argument / Parameter but does not Return any Value, which is evident from 
      • (number) Brackets 
    • In Function Prototype, Function Header and Function Call, we have (number) Brackets i.e., 
      • Function Header and Function Call Matches Function Prototype 
  • Conclusion 
    • When a Function takes an Argument / Parameter but does not Return any Value, in Function Call we use
      • Function Name with (number)
Step 7.2: Summary of Analysis – Calling a Function (No Return Type, Arguments / Parameters)
Analysis Parameters
Function Prototype – No Return Type, Arguments / Parameters
Function Prototype
void square02(number);
Function Call
square02(number);
Function Header 
def square02(number)
Body of Function 
Contains Code for Input + Processing + Output 
Output of Python Program

Enter an Integer Number: 6

Square of 6 = 36 

Execution of Input + Processing + Output
Input + Processing + Output is carried out in the Called Function (i.e., square02(number) Function)
Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, square02(number) Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Square of an Integer Number on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.,
    • Calculating and Displaying Square of an Integer Number on the Output Screen was successfully accomplished using square02(number) Function. Alhamdulillah (الحمدللہ) ��
  • Comparing Two Variations of Prototype 
Analysis Parameters
No Return Type, No Arguments / Parameters
No Return Type, Arguments / Parameters
Function Prototypevoid square01();void square02(int);
Function Callsquare01();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 + OutputInput + 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
  • Example 3 - Steps (Solving Programming Tasks using Functions)
Step 1: Completely and Correctly Understand the Real-world Task
Step 1.1: Real-world Task
  • Calculate the Square of an Integer Number 
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants them to practice the concept of squaring an Integer Number. 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 Square of an Integer Number 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
    • An Integer Number
  • Output
    • Square of the given Integer Number
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Number = 10
    • Output
      • 100
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Number = 7
    • Output
      • 49
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Number = 5
    • Output
      • 25
Step 2.1.2: Conclusion
  • Yes, we MAY Treat Real-world Task as a Programming Task
Step 2.2: Convert the Real-world Task Description (Step 1.2) into Programming Task Description
  • Write a Python Program takes an Integer Number as Input from User, calculates its square and displays the result on the Output Screen. 
  • Note – Please implement this using a Function (called square03()). 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 Square of an Integer Number 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 Integer Number as Input from User
    • Requirement 2
      • Display the Square of Integer Number on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Only take Integer Number as Input from User
    • Only take Integer Number as Input from User (Floats are not allowed)
  • Analysis of Requirement 2 – Display the Square of an Integer Number 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
      • An Integer Number (called number)
    • Instruction(s)
      • Calculate Square of Integer Number
      • Display Square of Integer Number on the Output Screen
  • Output
    • number * number 
  • Processing
    • Use * Operator to calculate Square of Integer Number – Multiply Integer Number with itself
    • Use print() Function  – To display the Square of Integer Number on the Output Screen
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 1
    • Data Type of Input = Integer
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = Integer
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be square03() and Function Prototype should have: Return Type, No Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • int square03();
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 square03() Function, which takes an Integer Number as Input from User, calculate its square and return the result (Square of Integer Number) to the main() Function. The main() Function will receive the result (Square of Integer Number) returned by the square03() Function and display it on the Output Screen.
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.

  • Step 1:  Execute Right Hand Side (RHS) of the Assignment Statement i.e., square03(). Note that  square03() is a Function Call.
  • Step 2:  Once Function Call (i.e., square03()) is complete, it will return an Integer value on RHS OF THE Assignment Statement i.e., square03() 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 = square03()

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

  • Calculation

·        result = number * number

·        result = 6 * 6

·        result = 36

  • Note

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

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

Store the Integer Value (Output) Returned by square03() in Variable result

  • Calculation
    • result = square03()
    • result = 36

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 

  • Function Prototype 
    • int square03();
  • Function Header
    • def square03():   
  • Function Call
    • square03()
  • Analysis 
    • The square03() 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 (Return Type and No Arguments / Parameters)
Analysis Parameters
Function Prototype – Return Type, No Arguments / Parameters
Function Prototype
int square03();
Function Call
square03();
Function Header
def square03()
Body of Function
Contains Code for Input + Processing + Output 
Output of Python Program

Enter an Integer Number: 6

Square of 6 = 36 

Execution of Input + Processing + Output
Input + Processing + Output is carried out in the Called Function (i.e., square03() Function)
Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, square03() Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Square of an Integer Number on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.
    • Calculating and Displaying Square of an Integer Number on the Output Screen was successfully accomplished using square03() 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 square01();void square02(int);void square03();
Function Callsquare01();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 + OutputInput + 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 ParametersNo Return Type, No Arguments / ParametersNo Return Type, Arguments / ParametersInput + 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
  • Example 4 - Steps (Solving Programming Tasks using Functions)
Step 1: Completely and Correctly Understand the Real-world Task
Step 1.1: Real-world Task
  • Calculate the Square of an Integer Number 
Step 1.2: Real-world Task Description
  • Ms Samavi is teaching Math to Grade II students. She wants them to practice the concept of squaring an Integer Number. 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 Square of an Integer Number 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
    • An Integer Number
  • Output
    • Square of the given Integer Number
Step 2.1.1.1: Example 1 – Input and Output of the Real-world Task
  • Example 1 – Input and Output
    • Input
      • Number = 10
    • Output
      • 100
Step 2.1.1.2:  Example 2 – Input and Output of the Real-world Task
  • Example 2 – Input and Output
    • Input
      • Number = 7
    • Output
      • 49
Step 2.1.1.3:  Example 3 – Input and Output of the Real-world Task
  • Example 3 – Input and Output
    • Input
      • Number = 5
    • Output
      • 25
Step 2.1.2: Conclusion
  • Yes, we MAY Treat Real-world Task as a Programming Task
Step 2.2: Convert the Real-world Task Description (Step 1.2) into Programming Task Description
  • Write a Python Program takes an Integer Number as Input from User, calculates its square and displays the result on the Output Screen. 
  • Note – Please implement this using a Function (called square04(number)). 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 Square of an Integer Number 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 Integer Number as Input from User
    • Requirement 2
      • Display the Square of Integer Number on the Output Screen
Step 3.3: Requirements Analysis
  • Analysis of Requirement 1 – Only take Integer Number as Input from User
    • Only take Integer Number as Input from User (Floats are not allowed)
  • Analysis of Requirement 2 – Display the Square of an Integer Number 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
      • An Integer Number (called number)
    • Instruction(s)
      • Calculate Square of Integer Number
      • Display Square of Integer Number on the Output Screen
  • Output
    • number * number 
  • Processing
    • Use * Operator to calculate Square of Integer Number – Multiply Integer Number with itself
    • Use print() Function  – To display the Square of Integer Number on the Output Screen
Step 4.2: Write down Function Prototype
  • Input 
    • Number of Input(s) = 1
    • Data Type of Input = Integer
  • Output
    • Number of Output(s) = 1
    • Data Type of Output = Integer
  • Function Prototype 
    • As mentioned in Programming Task Description (Step 2.2), the Name of Function should be square04(number) and Function Prototype should have: Return Type, Arguments / Parameters. Therefore, the Function Prototype will be as follows 😊
      • int square04(number);
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 square04() Function and pass Integer Number as Argument / Parameter to it. The square04() Function, will receive the Argument / Parameter passed to it, calculate its square (called result). The squre04() Function will return the result to the main() Function. The main() Function will receive the result returned by the square04() Function and display it on the Output Screen.
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.

  • Step 1:  Execute Right Hand Side (RHS) of the Assignment Statement i.e., square04(). Note that square04() is a Function Call.
  • Step 2:  Once Function Call (i.e., square04()) is complete, it will return an Integer value on RHS OF THE Assignment Statement i.e., square04() 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

number = 6 

(In main() Function)

result 

(In main() Function)

Enter an Integer Number: 6

  • result = square04()

·        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

  • Program Control is with the square04() Function 
    • Function Header 
      • Step 1: Declare a Variable number 
      • Step 2: Store Value (i.e., 6) Passed by Calling Function (i.e., main() Function) in the Variable number (declared in Step 1)

        –

         –

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)

  • Calculation

·        result = number * number

·        result = 6 * 6

·        result = 36

  • Note

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

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

result = square04(number)

Store the Integer Value (Output) Returned by square04() in Variable result

  • Calculation

·        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 

  • Function Prototype 
    • void square04(number);
  • Function Header
    • def square04(number):   
  • Function Call
    • square04(number)
  • Analysis 
    • The square04(number) 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 ()
Step 7.2: Summary of Analysis – Calling a Function (Return Type and Arguments / Parameters)
Analysis Parameters
Function Prototype – Return Type, Arguments / Parameters
Function Prototype

int square04(number);

Function Call

square04(number);

Function Header 

def square04(number)

Body of Function 

Contains Code for Input + Processing + Output 

Output of Python Program

Enter an Integer Number: 6

Square of 6 = 36 

Execution of Input + Processing + Output

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

Step 7.3: Main Findings
  • Alhamdulillah (الحمدللہ) 😊, square04(number) Function efficiently and effectively performed our required Programming Task i.e.
    • Calculating and Displaying Square of an Integer Number on the Output Screen
Step 7.4: Conclusion
  • To conclude, our main goal i.e.,
    • Calculating and Displaying Square of an Integer Number on the Output Screen was successfully accomplished using square04(number) 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
No Return Type, Arguments / Parameters
Function Prototypevoid square01();void square02(int);void square03();void square04(int);
Function Callsquare01();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 + OutputInput + 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 ParametersNo Return Type, No Arguments / ParametersNo Return Type, Arguments / ParametersInput + 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 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 01

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called cube()) which takes a number as Input from User and prints its Cube on the Output Screen.

Required Output

Enter an Integer Number: 2

Cube of 2 = 8

 

                        Task 02

Completely and Correctly Understand the Task

Task Description

  • Write a Function (called calculate_zakat()) which takes Current Savings of wealth as Input and prints amount of Zakat on the Output Screen.
    • Zakat = 2.5% of Current Savings 

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

  • Write a Function (called calculate_reward_on_sadqa()) which takes amount of Sadqa given by a person and prints amount that Allah will give in reward of Sadqa (on the Output Screen).
    • Please consider the minimum reward for Sadqa i.e., Allah will give 10 times 
    • Note that there is no upper bound on reward given by Allah on sadqa 

Required Output

Enter amount of Sadqa: 200

Allah will give you 10 times of 200 = 2000




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 23 - Functions in Python
  • Previous
Chapter 25- A Step by Step Example to Write Functions II
  • Next
Share this article!
Facebook
Twitter
LinkedIn

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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.