VBScript functions allow us to reuse our code again and again. For example we can write a function that performs the concatenation of two strings. After writing the function whenever we need the functionality of the concatenation of two strings, we can CALL our string concatenation function.
VBScript functions are defined using the Function and End Function keywords. The code that makes up the function is written between these two keywords.
Here are the steps to write VBScript functions:
Here is a simple example that requires two numbers as arguments and sums them both:
Function add(num1, num2)Calling VBScript Functions
add = num1 + num2
A function doesn't do anything until it is called. Calling a function in VBScript is very easy. We call a function by writing its name followed by a pair of brackets. Optionaly, if the function requires external information, we have to pass it between the pair of brackets.
Here is a simple Example of calling our add function, above:
In the above line of code we call our add function, passing in two values, also called parameters.
Here is an example:
Here is the output:
The total of 10 and 5 is: 15
Next >>> Lesson No. 14: VBScript Message Box