JANOS Help System: [Commands] [Topics] [Tech Support] [Printable Manual] [Search]
Functions Scripting DESCRIPTION Functions are sections of code that perform a specific task. A function contains program statements that while written only once can be invoked multiple times, as many as is needed. The function takes parameters which are the variables upon which the task is performed. Very importantly the function returns a value as a result. A Function always returns a value even if none is required. In that case a value of NULL would be returned. Typically a function will return the result of a calculation or other operation. The CKSUMS scripting example utilizes a function whose purpose is to format and output a text string as an ECHO command for proper batch operation. This function does not return anything of use. It is just used to repeat an output operation in a defined manner. This is a custom function. USER-DEFINED FUNCTIONS Any number of functions may be defined in developing script. Each function may have zero or more parameters and may optionally return a result. A function need not be defined in a script before it is invoked. Function functionName($param1, $param2, ...) { ... program statements ... return $result; } Function names consist of one or more characters from the set [_a-zA-Z0-9] where the first character cannot be a digit [0-9]. This is similar to the restriction on variable names. ARGUMENTS Functions may be defined with any number of arguments or none at all. A call to the function provides values for the parameters. The call may supply fewer parameters than provided in the function definition in which case the additional defined parameters will receive a NULL value or may be defaulted in the function definition as follows. If too many arguments are supplied the additional will be ignored. function foo($arg, $str = 'default') { // function body return $ret; } RETURNED VALUES A function may return a value using a return statement as shown in the examples above. If the function completes without executing a return statement a NULL value is returned. Any number of return statements may appear anywhere in the function body. VARIABLE SCOPE Each function has its own local variable scope. Variables defined in a function are available only in that function. A variable may be defined with the same name as a global variable (those available to the main program) and not affect or otherwise corrupt the global value. Global variables are not accessible by default within a function. GLOBAL VARIABLE REFERENCES Global variables are those defined in the top-level program. They can be accessed from a function using the global statement. global $gvar1, $gvar2, [..., $gvarN]; The global statement creates a alias for the global variable in the local scope. Subsequent references to the variable retrieve the global variable value and the global variable may be modified. For example: $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; The above when executed will output the value 3. RECURSION Functions may call other functions and may be used recursively. Functions may define other functions and may redefine themselves. NOTES If you need to conditionally define a function then it must be defined before it is referenced. This would assure that the proper form of the function is used. Otherwise results may not be as expected. SEE ALSO HELP Topics: CKSUMS, VARIABLES, SCRIPT [/flash/manpages/program.hlp:600]