VBScript functions are self contained blocks of code that perform a given "function", then return a value.
Sometimes when coding, you may find yourself having to write the same piece of code over and over again. If this happens, chances are, you'd be better off writing a function. That way, you can write the code once, then simply call the function whenever you need it.
Creating VBScript Functions
VBScript functions are defined using the Function and End Function keywords. The code that makes up the function resides between these two keywords.
Here's the method for creating VBScript functions:
- Start by using the Function keyword. This tells the browser that a function is about to be defined
- Provide a name for the function (make it concise, but descriptive)
- Follow the name with opening and closing brackets
- Add the names of any arguments that the function requires (optional)
- Starting on a new line, write the code that makes up the function
- Finish with End Function
Calling VBScript Functions
A function doesn't actually do anything until it is called. Once it is called, it takes any arguments, then performs it's function (whatever that may be).
You call a VBScript function like this:
- Write the function name, followed by opening and closing brackets
- Between the brackets, provide all arguments that the function requires
Combined - Creating, then Calling a VBScript Function
So, if we combine them together, and add a tiny bit more code, we might end up with something that looks like this:
Using the above code, the following is produced: