7.1.8 Function
- Purpose
-
Declares the name and arguments, and indicates the start of the Function procedure. It also defines a sequence of statements enclosed by the Function statement and End Function statement as a Function procedure.
- Syntax
Function Name [(ArgList)] [Statements] [Name = Expression] [Exit Function] [Statements] [Name = Expression] End [Function]
- Arguments
-
- Name
-
Write the name of the Function procedure you are defining. Comply with the variable naming conventions.
- ArgList
-
Specify a list of variables representing the arguments to pass to the Function procedure when it is called. Delimit multiple variables with commas. Enclose any arguments in parentheses.
- Statements
-
Write a group of statements to be executed within the Function procedure.
- Expression
-
Write the return value of the Function procedure.
- Description
-
Function procedures are public and can be referenced by all other procedures in the script.
The executable code must be written entirely within the procedure. You cannot define a Function procedure inside another Function procedure or inside a Sub procedure.
The Exit Function statement causes the program to immediately exit from a Function procedure. Execution continues from the statement following the statement that called the Function procedure. You can write any number of Exit Function statements anywhere in a Function procedure.
For details on calling Function procedures, see 7.1.10 Call.
To return a value from a Function procedure, assign the value to the Name of the Function procedure. You can assign any number of values to Name anywhere within the procedure. If no value is assigned to Name, the procedure returns a zero-length string ("") as a set return value.
Two types of variables can be used in Function procedures: variables that are explicitly declared within the procedure and those that are not. Variables that are explicitly declared in a procedure, using the Dim command or equivalent, behave as local variables that are valid only within that procedure. Variables that are used but not explicitly declared in a procedure are also local unless they are explicitly declared at some higher level outside the procedure. The value of local variables in a Function procedure are discarded when the procedure finishes execution.
A variable that is not explicitly declared in the procedure can be used in a Function procedure, but it will be handled as a global variable if another variable or entity is defined at the script level with the same name as the undeclared variable.
- Example
' Compare the input data and display the results in ascending ' order. Dim compnum1,compnum2 InputBox ( "Enter the numeric values to be compared.","Input data", _ 100, 100, compnum1, "numeric value 1" ,compnum2, "numeric value 2" ) If NumberCompare( compnum1 , compnum2) <> True Then lower = compnum2 upper = compnum1 Else lower = compnum1 upper = compnum2 End If Messagebox( "lower=[" & lower & "] , upper=[" & upper & "]" ) Function NumberCompare(p1, p2) NumberCompare =True Dim lower,upper lower = p1 upper = p2 If lower > upper Then NumberCompare = False Exit Function End If End Function
- JP1/Script version
-
Supported from JP1/Script 01-00.