VB Script Procedures and Functions:
Both procedures and functions are basic block of codes which gives the modularization concept as well as re-usability concept.They allow us to break down the larger complex code into small reusable chunk of codes.
The block of code is a logical grouping of related tasks.They have certain structure. Calling a named block with or without argument is a contract between user and the named block.Once the contract is fulfilled the user can call the block of code to perform the action.
This approach of modularization increases the readability, re-usability and understandability . It is a very important concept while building automation framework.
In VBScript, there are two kinds of modularization techniques available.They are:
- Sub procedure
Naming convention of Sub and Functions:
- The name should be clear.
- The name should have a purpose.
- We need to use verb Noun composition to create a name.
A Sub procedure is a named block of a series of VBScript statements (enclosed by Sub and End Sub statements) that perform actions but don’t return a value.
A Sub procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure).
If a Sub procedure has no arguments, its Sub statement must include an empty set of parentheses ().There is an alternative implementation also. It says that if the sub does not have any argument,there is no need to provide parentheses(). We can follow any one approach.
Syntax of Sub:
(public|private )Sub ProcedureName () Statements ----------- ----------- End Sub
(public|private )Sub ProcedureName(argument1, argument2) Statements ----------- ----------- End Sub
Sub ConvertTemp() temp = InputBox("Please enter the temperature in degrees F.", 1) MsgBox "The temperature is " & Celsius(temp) & " degrees C." End Sub
A Function is a named block of a series of VBScript statements enclosed by the Function and End Function statements.
A Function procedure is similar to a Sub procedure, but can also return a value.The return type of a function is a variant.We can define the datatype as well.learn more about datatype to understand return type.
A Function procedure can take arguments (constants, variables, or expressions that are passed to it by a calling procedure).
If a Function procedure has no arguments, its Function statement must include an empty set of parentheses.
A Function returns a value by assigning a value to its name in one or more statements of the procedure. The return type of a Function is always a Variant.However returning a value from function is purely an optional step.
Syntax of function:
(public|private)Function FunctionName () Statements ----------- ----------- FunctionName =returnValue End Function
(public|private)Function FunctionName (argument1, argument2....argumentN) Statements ----------- ----------- FunctionName =returnValue End Function
Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function
Function cal(a,b,c) cal = (a+b+c) End Function
Why Public or Private is needed?
Public or Private are access control.They are useful while working with class.However in a single .VBS file they does not imply anything. By default all functions or procedures are public. However while working with class they play a significant role.
Getting Data into and out of Procedures or function:
- Each piece of data is passed into our procedures using an argument.
- Arguments serve as placeholders for the data we want to pass into our procedure. We can name our arguments any valid variable name.
- When we create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure.
- Any arguments are placed inside these parentheses, separated by commas.
Using Sub and Function Procedures in Code:
A Function in our code must always be used on the right side of a variable assignment or in an expression.
Temp = Celsius(fDegrees)
MsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees."
To call a Sub procedure from another procedure, type the name of the procedure along with values for any required arguments, each separated by a comma.
The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses.
The following example shows two calls to the MyProc procedure. One uses the Call statement in the code; the other doesn’t. Both do exactly the same thing.
Call MyProc(firstarg, secondarg) MyProc firstarg, secondarg
Notice that the parentheses are omitted in the call when the Call statement isn’t used.
- We can call a sub from function and alternatively we can call a function from Sub.
- VBscript does not support varargs as java but supports optional arguments.All functions and subs can take optional argument and optional argument must appear at last in the argument list.
- For arguments we need to mention the mandatory argument first followed by optional arguments.
How to exit from Function or Sub?
The keyword to exit from function is Exit Function and Sub is Exit Sub.The exit Function or Exit Sub keyword will terminate the execution and come out of the function or sub.Once the Exit Function or Exit sub is encountered by WSH the control goes back to the position from where the function or procedure is called with a return value in case of Function.
In general Exit Function and Exit sub does not play a significant role but while using in a loop, they become super relevant.