Functions in VBScript
The second type of procedure is called a function. Like a subroutine, a function also holds a series of VBScript statements. The only difference is that a function actually returns a value to the code statement that called it. A function is a block of code that can be called from anywhere in a program to accomplish a specific task. Functions can accept parameters and can also return a result code to the caller.
You can fill a variable with a value supplied on the right side of an assignment statement:
As before, make sure the name of the function adequately describes what the function does. The same naming conventions that apply to the subroutine also apply to the function. Also, arguments are passed to the function the same way they are passed to a subroutine.
VBScript automatically knows that your intention is to pass the value back to the caller, not to create a temporary variable within the function. If you don't set this "variable" in your program, the result will be a zero or an empty string. It's very important, therefore, that you make sure your function returns some valid value. This value should be assigned to the function name itself within the block of code that makes up the function.
This function contains a loop that asks the user for his age and continues to ask the user until the age falls within the correct range. Once it does, the condition of the control structure is no longer met and the loop completes.
Even though this implementation would work just fine, it's a good programming practice to use a temporary variable and then make the assignment at the end. This is useful in part because it's easier to read the code. The reader can make better sense of your code when every variable is declared and the function uses no variables that do not have declarations. Because the function "variable" has no formal declaration and could potentially be set in several different code branches within the function, it might confuse the surveyor of your code if your code is lengthy.
Another important point to keep in mind is that when you write a function that uses arguments, you must not declare variables with the same name as the arguments using the Dim statement. When you specify arguments in a function, those arguments are automatically declared as variables in the function, but they are set to whatever was supplied when the procedure is called. If you try to declare them again, you will get an error because they're already in use in the function.
You need to know how to call it. The benefit of using a function is that you can pass back a piece of data to the caller. The subroutine does not enable you to do this because it does not return anything. You will see a way to change variables in the calling code with a subroutine later today, but the function is a better way to get data back and forth. To call a function, you simply use the syntax
For an example of its use, suppose you have a function called GetAge. To use the GetAge function, you could enter the statement
To exit a function, you use the same method as when you exit a subroutine, namely the End Function statement. This statement can only appear once at the end of the function declaration. You have seen this statement used in the functions discussed so far. You can also use the statement Exit Function to break out of a function just like you used the Exit Sub statement to exit a subroutine. As before, it's better to exit a function naturally when your code reaches the final End Function statement than to use an Exit Function line of code to terminate the function in the middle of the statements. The code is simply easier to follow when you avoid such forced exit statements.
|All times are GMT +5.5. The time now is 03:19.|