Function

Define a function procedure.

Syntax 
      [Public [Default] | Private ] Function name([arg_List]) 
         [statements]
         [name=expression]
         Exit Function
            [statements]
            [name=expression]
      End Function

Key
   Public          Extend the scope of this function to all procedures in the project.

   Public Default  Define a method as the default member of a class
                   (Only for public functions defined within a class.)

   Private         Restrict the scope of this function to procedures within the same module.

   name            The name of the function.
 
   arg_List        Argument variabless passed to the function, comma separated.
                   By default, each local variable=argument (ByRef)
                   To have each local variable=value of the argument prefix the 
                   argument with 'ByVal'.

   statements      Program code

   expression      The value to return.

In VBScript, functions can be defined before or after the code that calls it. In many other languages (e.g. PowerShell) it is required to define the function before it is called. Placing the definitions first does have the advantage that the finished code can be read from top to bottom without having to jump up and down the page.

Examples

Function DemoFunc(Arg1, ByRef Arg2)
    ' Return the two arguments in a single string
    DemoFunc = "First: " & Arg1 & " second: " & Arg2
End Function

'Now call the function above
myDemo = DemoFunc("Hello","World")
wscript.echo myDemo

“The most important thing in an argument, next to being right, is to leave an escape hatch for your opponent, so that he can gracefully swing over to your side without too much apparent loss of face” ~ Sydney J. Harris

Related VBScript commands

Sub - Start a subprocedure.
IsMember.vbs - Function to determine group membership.
Equivalent in PowerShell: Functions


 
Copyright © 1999-2024 SS64.com
Some rights reserved