|
|
|
A Function procedure works just like a Sub (subroutine) procedure. Like a subroutine, a function also holds a series of VBScript statements. It can take arguments and call other procedures. The only difference is that a function actually returns a value to the code statement that called it. The value returned can be then processed by the calling procedure. Defining functionsTo declare a function, use the Function keyword instead of the Sub keyword. You end functions using the End Function statement. The structure of a function is <SCRIPT> ...... Function function_identifier([parameter list]) *** VBScript Code Here *** End Function ...... </SCRIPT> where function_identifier is the name of the function and [parameter list] are optional arguments you can pass to the function. As with the subroutine, the parentheses are not required if no arguments are passed to the function. When you create and use a function, the return value of the function is held by a variable with the name of the function. For example, if you create a function that converts seconds to minutes, you might name the function Minute: Function Minute(seconds) Minute=seconds/60 End Function As in subroutines 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. Calling FunctionsAs with the subroutine, the flow of code is redirected to the function while the code within the function executes. Once the function has finished executing, control returns back to the code that called the function, the value from the function is assigned to the calling code, and execution picks up from there. 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 in the next lessons, but the function is a better way to get data back and forth. To call a function, you simply use the syntax return_variable = function_identifier([parameter list]) Notice that in this case, the syntax is quite a bit different from the subroutine. Here, you can assign the function to a variable (or another expression that can be updated with a value), or you needn't assign it to anything. The parentheses are optional only when no arguments are passed to the function. Go To Page: 1 2
The copyright of the article Procedures in VBScript. Functions in VB Script is owned by . Permission to republish Procedures in VBScript. Functions in print or online must be granted by the author in writing.
|
|
|
|
|
|
|
|