Download presentation
Presentation is loading. Please wait.
Published byChristian Wilkins Modified over 8 years ago
1
Introduction to Computing Dr. Nadeem A Khan
2
Lecture 21
3
Sub Command1_Click ( ) Sub Command1_Click ( ) Dim x As Integer, y As Integer, num As Integer Let x=2 Let y=3 Let num = Val(Text1.Text) Select Case num Case y-x, x Picture1.Print “Buckle my shoe.” Case Is<=4 Picture1.Print “Pick up sticks.” Case x+y To x*y Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub
4
Read Chapter 5 completely!
5
Subprograms /Subroutines
6
Subprograms (Contd.) ► General Format Sub SubprogrammeName (list of parameters) statements End Sub
7
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
8
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
9
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
10
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
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.) ► 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
13
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
14
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
15
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
16
► Write a subroutine to reverses the input string Sub Command1_Click ( ) Dim nom As String Picture1.Clsnom=Text1.TextReverse(nom) End Sub Example: For Next Loop - SubProgram
17
Sub Reverse(info As String) Dim m As Integer, j As Integer, temp As String Let temp = “” For j = Len(info) To 1 Step -1 Let temp = temp + Mid$(info, j, 1) Next j Picture1.Print temp End Sub Example: For Next Loop - SubProgram
18
Local Variable and Form Level Variable
19
Subprograms: Local Variables ► Example 1 Sub Command1_Click( ) Picture1.Cls Call Three End Sub Sub Three( ) Dim num As Single Picture1.Print num Let num=3 Picture1.Print num End Sub
20
Subprograms: Local Variables ► Example 1: Result 0 3 0 3 => The variable num is local to the Subprogram; Lives only for the time the Subprogram is called; Initialized each time
21
Subprograms: Local Variables ► Example 2 Sub Command1_Click( )Sub Trivial ( ) Dim x As SingleDim x As Single Picture1.ClsPicture1. Print x; Let x = 2Let x = 3 Picture1.Print x;Picture1.Print x; Call TrivialEnd Sub Picture1.Print x; Call Trivial Picture1.Print x; End Sub
22
Subprograms: Local Variables ► Example 1: Result 2 0 3 2 0 3 2 => The variable x has separate identities in the Event procedure and in the subroutine
23
Subprograms: Form-Level Variables ► Example 1 Dim num1 As Single, num2 As Single Sub Command1_Click( )Sub AddAndIncrement ( ) Let num1 =2Picture1.Print num1+num2 Let num2 =3Let num1= num1+1 Picture1.ClsLet num2=num2+1 Call AddAndIncrementEnd Sub Picture1.Print Picture1.Print num1 Picture1.Print num2 End Sub
24
► Example 1: Result 534 => The variable num1 and num2 known to all subprograms subprograms Subprograms: Form-Level Variables
25
► Example 2 Dim pi As Single Sub Form_Load( ) Let pi = 3.14159 End Sub Sub Command1_Click ( ) Picture1.Cls Picture1.Print “Circle area is:” pi*5^2 End Sub
26
► Example 2: Result Circle area is: 78.53975 => The variable pi is initialized in Form_load event Subprograms: Form-Level Variables
27
Subprograms: Passing Variables ► Example 1: Subroutine Sub Triple( num As Single) Rem Triples a number Picture1.Print num; Let num = num*3 Picture1.Print num; End Sub
28
Subprograms: Passing Variables ► Example 1: Program Sub Command1_Click( ) Dim amt As Single Picture1.Cls Let amt=2 Picture1.Print amt; Triple(amt) Picture1.Print amt End Sub
29
Subprograms: Passing Variables ► Example 1: Result 2 2 6 6 => Variable was passed by reference in Example 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.