VBSCRIPT PROCEDURES

  • A procedures is a segment of code grouped into a single entity.
  • can re-use them.
  • By doing so,reduces  the need  to re-type the same code over and over again.
  • Once we implement a procedure, we  can use its code by calling that procedure.

Sub procedures:

A Sub procedure can perform some actions, but CANNOT return values.
A Sub procedure is enclosed in Sub and End Sub.

Syntax:

Sub nameOfSubProcedure()
actions to perform
End Sub

Example:

Sub displayText()
 document.write("Here is some text");
End Sub

Using parameters:

Parameters are variables placed inside the parentheses of a sub procedure which are used by the code inside the sub procedure in some way.
 Parameters are separated by commas.

Using parameters with a Sub procedure

Syntax:

Sub nameOfSubProcedure(param1, param2, etc.)
actions to perform
End Sub

Example:

Sub displayName(name)
document.write("Your name is " & name);
 End Sub

Calling a Sub procedure

To call a Sub procedure, refer to the name of the Sub procedure with the Call statement, and pass the appropriate parameter values to it (if there are any).

Syntax:

Call subProcedureToCall()

Example:

Sub printMessage()
document.write("VBScript is a scripting language.")
End Sub
Call printMessage()

Output:

VBScript is a scripting language.





No comments:

Post a Comment