Download presentation
Presentation is loading. Please wait.
Published byCatherine Anthony Modified over 9 years ago
1
Sub Procedures
2
A Sub procedure is a block of code that is executed in response to an event. There are two types of Sub procedures, general procedures and event procedures.
3
General Sub Procedures A general procedure tells the application how to perform a specific task. Once a general procedure is defined, it must be specifically invoked by the application. By contrast, an event procedure remains idle until called upon to respond to events caused by the user.
4
Why create general procedures? several different event procedures might need the same actions performed eliminates the need to duplicate code makes the application easier to maintain may be able to reuse general procedures in other applications
5
Sub Procedure Syntax Syntax for Sub procedure declaration: Private Sub ProcedureName() statements End Sub Syntax for calling Subroutines: Call ProcedureName
6
Private Sub cmdAdd_Click() Dim num1 As Single, num2 As Single 'Display the sum of two numbers picResult.Cls picResult.Print "This program displays a sentence" picResult.Print "identifying two numbers and their sum." picResult.Print num1 = 2 num2 = 3 picResult.Print "The sum of"; num1; "and"; num2; "is"; num1 + num2 End Sub
7
Private Sub cmdAdd_Click() Dim num1 As Single, num2 As Single 'Display the sum of two numbers picResult.Cls Call ExplainPurpose picResult.Print num1 = 2 num2 = 3 picResult.Print "The sum of"; num1; "and"; num2; "is"; num1 + num2 End Sub Private Sub ExplainPurpose() 'Explain the task performed by the program picResult.Print "This program displays a sentence" picResult.Print "identifying two numbers and their sum." End Sub
8
Using Parameters with Sub Procedures Syntax for defining a Sub procedure with parameters: Private Sub ProcedureName(Parameters) statements End Sub The parameters for a procedure are like a variable declaration, declaring values that are passed in from the calling procedure. Private Sub Add(num1 As Single, num2 As Single) 'Display numbers and their sum picResult.Print "The sum of"; num1; "and"; _ num2; "is"; num1 + num2 End Sub
9
Calling Sub Procedures with Arguments Syntax for calling a Sub procedure with Arguments: Call ProcedureName (value1, value2, … ) Arguments send values to the parameters in the Sub Procedure from the calling procedure. Private Sub cmdAdd_Click() 'Display the sum of two numbers picResult.Cls Call ExplainPurpose picResult.Print Call Add(2, 3) End Sub
10
Private Sub cmdAdd_Click() 'Display the sum of two numbers picResult.Cls Call ExplainPurpose picResult.Print Call Add(2, 3) End Sub Private Sub Add(num1 As Single, num2 As Single) 'Display numbers and their sum picResult.Print "The sum of"; num1; "and"; num2; "is"; num1 + num2 End Sub Private Sub ExplainPurpose() 'Explain the task performed by the program picResult.Print "This program displays a sentence" picResult.Print "identifying two numbers and their sum." End Sub
11
Private Sub cmdAdd_Click() 'Display the sums of several pairs of numbers picResult.Cls Call ExplainPurpose picResult.Print Call Add(2, 3) Call Add(4, 6) Call Add(7, 8) End Sub Private Sub Add(num1 As Single, num2 As Single) 'Display numbers and their sum picResult.Print "The sum of"; num1; "and"; num2; "is"; num1 + num2 End Sub
12
Private Sub cmdAdd_Click() Dim Value1 As Single, Value2 As Single Value1 = Val(Text1.Text) Value2 = Val(Text2.Text) picResult.Cls Call ExplainPurpose picResult.Print Call Add(Value1, Value2) End Sub Private Sub Add(num1 As Single, num2 As Single) 'Display numbers and their sum picResult.Print "The sum of"; num1; "and"; num2; "is"; num1 + num2 End Sub
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.