Download presentation
Presentation is loading. Please wait.
1
Introduction to Computing Dr. Nadeem A Khan
2
Lecture 17
3
Subprograms /Subroutines
4
Subprograms (Contd.) ► General Format Sub SubprogrammeName (list of parameters) statements End Sub
5
Subprograms (Contd.) ► Example 1 Sub ExplainPurpose ( ) Rem Explain the task performed by the program Picture1.Print “This program displays sentences” Picture1.Print “identifying pairs of numbers and their sums.” End Sub
6
Subprograms (Contd.) ► Example 2 Sub Add ( num1 As Single, num2 As Single ) Rem Display numbers and their sum Picture1.Print “Sum of”; num1;”and”; num2; ”is”; num1+num2 End Sub
7
Subprograms (Contd.) ► Examples: Sub ExplainPurpose ( ) Rem Explain the task performed by the program Picture1.Print “This program displays sentences” Picture1.Print “identifying pair of numbers and their sums.” End Sub Sub Add ( num1 As Single, num2 As Single ) Rem Display numbers and their sum ► Picture1.Print “Sum of”; num1; “and”; num2; “is”; num1+num2 End Sub
8
Subprograms (Contd.) ► Using subprograms in a Program Sub Command1_Click( ) Rem Display the sum of several pairs of numbers Picture1.Cls Call ExplainPurpose Picture1.Print Call Add(2, 3) Call Add(4, 6) Call Add(7, 8) End Sub
9
Subprograms (Contd.) ► The Program’s result This program displays sentences identifying pair of numbers and their sums. Sum of 2 and 3 is 5 Sum of 4 and 6 is 10 Sum of 7 and 8 is 15
10
Subprograms (Contd.) ► Using subprograms in a Program Sub Command1_Click( ) Dim x As Single, y As Single, z As Single Picture1.Cls Let x = 2 Let y = 3 Call Add(x,y) Call Add(x+2, 2 * y) Let z = 7 Call Add(z, z+1) End Sub
11
Subprograms (Contd.) ► The Program’s result This program displays sentences identifying pair of numbers and their sums. Sum of 2 and 3 is 5 Sum of 4 and 6 is 10 Sum of 7 and 8 is 15
12
Subprograms (Contd.) ► Writing subprograms for a Program Sub Command_Click( ) Dim x As Single, y As Single, z As Single Picture1.Cls Call ComputeWage(“Aslam”, 5, 100) Call ComputeWage(“Saleem”, 6, 90) End Sub Result: Wage of Aslam is Rs. 500 Wage of Saleem is Rs. 540
13
Subprograms (Contd.) ► The Subprogram ComputeWage: Sub ComputeWage(Name As String, hourlyRate As Single, hours As Single) Rem Compute and displays employees wage Dim wage As Single wage = hourlyRate*hours Picture1. Print Name;” wage is”; wage End Sub
14
Note: ► Read: Schneider (Section 4.1) ► Read Comments in Section 4.1 ► Do practice problems 4.1 ► Attempt questions of Exercise 4.1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.