Declares the name, arguments, and code that form the body of a Function procedure.


A Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. A Function procedure can be used on the right side of an expression in the same way you use any intrinsic function, such as Len or Asc, when you want to use the value returned by the function.



Function name [(argument1, argument2, [optional]argumentx)]


   [name = expression]





Name of the Function. Note that the name must be unique and can not be the same as a label within the same scope.


List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas.  All arguments are passed by value. If an argument is preceded by the OPTIONAL keyword, the argument is not required, and all subsequent arguments in the list must also be optional and declared using the OPTIONAL keyword.


Any group of statements to be executed within the body of the Function procedure.


Return value of the Function.


Function procedures have a global scope, that is, they are visible to all other scripts and procedures in the scripts. The value of local variables in a Function is not preserved between calls to the procedure.


You cannot define a Function procedure inside another procedure.


The Return statement causes an immediate exit from a Function procedure. Program execution continues with the statement that follows the statement that called the Function procedure. Any number of Return statements can appear anywhere in a Function procedure.


You call a Function procedure using the function name, followed by the argument list in parentheses, in an expression.


Note:  Function procedures can be recursive, that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow.


To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure. If no value is assigned to name, the procedure returns an empty value.



Variables used in Function procedures fall into two categories: those that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure (using Dim) are always local to the procedure. Variables that are used but not explicitly declared in a procedure are global.


Note: a procedure can use a variable that is not explicitly declared in the procedure, but a naming conflict can occur if anything you have defined at the script level has the same name. If your procedure refers to an undeclared variable that has the same name as another procedure or variable, it is assumed that your procedure is referring to that script-level name.




Function ReadNC( ServerName )

  $ReadNC = ""

  $Root = GetObject( "LDAP://" + ServerName + "/rootDSE" )

  If @ERROR = 0

     $ReadNC = $Root.defaultNamingContext