Scripting > mrScriptBasic overview > mrScriptBasic language reference > Procedures > Function
 
Function
Declares the name, arguments, code, and return value that form the body of a Function procedure.
Syntax
Function name([<arglist>])
[statements]
[name = expression]
[Exit Function]
[statements]
[name = expression]
End Function
<arglist> ::= varname[[]]
Arguments
name
Name of the Function. See Naming conventions.
statements
Any group of statements to be executed within the body of the Function procedure.
varname
Name of the variable representing the argument. See Naming conventions.
expression
An expression that defines the return value of the Function procedure.
Remarks
A Function procedure is a separate procedure that can take arguments, perform a series of statements, change the value of its arguments, and return a value. Unlike a Sub procedure, a Function procedure returns a value and can be used in an expression.
A Function procedure is called using the procedure name followed by the argument list, which must be enclosed in () (parentheses). Simple arguments are passed by value, which means that updates made in the procedure are not reflected in the calling script. Array and object arguments are passed by reference, which means that any updates made to those variables in the procedure are seen in the calling script.
It is not possible to define a Function procedure inside any other procedure (such as Function or Sub).
The Exit Function 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 Exit Function statements can appear anywhere in a Function procedure.
Notes
Function procedures can be recursive, calling themselves to perform a given task. However, recursion can lead to stack overflow if used without undue care.
Variables defined in the script block that calls the function are not visible inside the function unless you pass them as arguments.
Example
The following example shows a Function procedure used in a DataManagementScript (DMS) file cleaning routine to check whether more than one response has been chosen for a single response question:
Dim intFlag
intFlag = CheckForMulti(age)
If intFlag = 1 Then
Job.DropCurrentCase()
End If

Function CheckForMulti(IncomingVariable)
If IncomingVariable.AnswerCount() > 1 Then
CheckForMulti = 1
Else
CheckForMulti = 0
End If
End Function
See also
Procedures