Functions in VBScript

Discussion in 'JavaScript and AJAX' started by Sanskruti, May 7, 2007.

  1. Sanskruti

    Sanskruti New Member

    Jan 7, 2007
    Likes Received:
    Trophy Points:
    Software Consultant
    Mumbai, India


    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:
    ZipCode = 99502 
    In the same manner, you can fill a variable with a value supplied by a function you define:
    ZipCode = GetZipCode("Anna") 
    As 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. To 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
    Function Function_Name(argument1, argument2, …, argumentn)
         ...code within the function 
    End Sub
    where Function_Name is the name of the function and argument1 through argumentn 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.

    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.

    Calling a 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

    return_variable = function_name(argument1, argument2, …, argumentn)
    Notice that in this case, the syntax is quite a bit different from the subroutine. Here, you can assign the function to a variable you needn't assign it to anything. The parentheses are optional only when no arguments are passed to the function.

    For an example of its use, suppose you have a function called GetAge. To use the GetAge function, you could enter the statement

    UserAge = GetAge() 
    UserAge = GetAge 
    Notice that this function doesn't need any arguments, and the result is assigned to a variable named UserAge.

    Exiting a Function

    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.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice