Definition: A sub procedure is a section of code that carries an assignment but doesn't give back a result. To create a sub procedure, start the section of code with the Sub keyword followed by a name for the sub procedure. To differentiate the name of the sub procedure with any other regular name, it must be followed by an opening and closing parentheses. The section of the sub procedure code closes with End Sub as follows
Syntax:
Sub ShowMeTheDough ()
Procedure Description
Procedure Description
Procedure Description
Procedure Description
End Sub
The name of a sub procedure should follow the same rules we reviewed for the variables, omitting the prefix:
• If the sub procedure performs an action that can be represented with a verb, you can use that verb to name it. Here are examples: show, display etc…
• To make the name of a sub procedure stand, you should start it in uppercase. Examples are Show, Play, Dispose, Close etc…
• You should use explicit names that identify the purpose of the sub procedure. If a procedure would be used as a result of another procedure or a control's event, reflect it on the name of the sub procedure. Examples would be: AfterUpdate, LongBefore.
Creating Procedure:
In the following example, a sub procedure named DisplayMyName is created. It retrieves fields of two text boxes (first name and last name) on a form and displays a full name as a result of combining them:
Sub DisplayMyName ()
FullName = FirstName & " " & LastName
End Sub
As mentioned already, you can declare variables for use in your program. In the same way, you can declare variables in the procedure if you need to. These variables are declared and dealt with in the same way we learned in the regular script sections. Using declared variables, the above procedure can be written as follows:
Sub DisplaMyName ()
Dim FirstName, LastName
Dim FullName
FullName = FirstName & " " & LastName
End Sub
Calling a Procedure:
After creating a procedure, you can call it from another procedure, function, or control's event in the body section of an HTML file. To call a simple procedure such as the earlier DisplayMyName, you can just write the name of the sub procedure.
In the following example, the above DisplayMyName sub procedure is called when the user clicks the ShowMe section of the form:
Sub ShowMe ()
DisplayMyName
End Sub
If you want the procedure to be accessed immediately as soon as the page displays, you can assign its name to the onLoad () event of the body tag.
Arguments
Passing an Argument:
To carry an assignment, sometimes a procedure needs one or more values to work on. If a procedure needs a variable, such a variable is called an argument. Another procedure might need more that one argument, thus many arguments. The number and types of arguments of a procedure depends on various factors.
If you are writing your own procedure, then you will decide how many arguments your procedure would need. You also decide on the type of the argument(s). For a procedure that is taking one argument, in the parentheses of the procedure, write a name for the argument. Here is an example:
Sub CalculateArea (Radius)
Dim dblPI
Dim dblArea
dblPI = 3.14159
dblArea = Radius * Radius * dblPI
End Sub
A procedure can take more than one argument. If you are creating such a procedure, between the parentheses of the procedure, write the name of the first argument followed by a comma; add the second argument and subsequent arguments and close the parentheses. There is no relationship between the arguments; for example, they can be of the same type:
Sub CalculatePerimeter (Length, Height)
Dim dblPerimeter
dblPerimeter = 2 * (Length + Height)
End Sub
The arguments of your procedure can also be as varied as you need them to be. Here is an example:
Sub DisplayGreetings(strFullName, intAge)
Dim Sentence
Sentence = "Hi,” & strFullName & ". You are” & intAge & “years old"
End Sub
Calling an Argumentative Procedure:
We saw already how to call a procedure that doesn't take any argument. Actually, there are various ways you can call a sub procedure. As we saw already, if a sub procedure doesn't take an argument, to call it, you can just write its name. If a sub procedure is taking an argument, to call it, type the name of the sub procedure followed by the name of the argument. If the sub procedure is taking more than one argument, to call it, type the name of the procedure followed by the name of the argument, in the exact order they are passed to the sub procedure, separated by a comma. Here is an example:
Sub Result ()
Dim dblHours, dblSalary
CalcAndShowSalary dblHours, dblSalary
End Sub
Sub CalcAndShowSalary(Hours, Salary)
Dim dblResult
dblResult = Hours * Salary
txtResult = dblResult
End Sub
Alternatively, you can use the keyword Call to call a sub procedure. In this case, when calling a procedure using Call, you must include the argument(s) between the parentheses. Using Call, the above procedure could call the CalcAndShowSalary as follows:
Sub Result ()
Dim dblHours As Double
Dim dblSalary As Double
dblHours = txtHours
dblSalary = txtSalary
Call CalcAndShowSalary(dblHours, dblSalary)
End Sub
No comments:
Post a Comment