Link Search Menu Expand Document

3.9 User-defined Function

You can define a function (user-defined function) that summarizes a series of processes. The function can take some arguments and return the processing result. Functions are similar to the methods that objects have, but they are not tied to a particular object. Also, unlike methods, there is no stipulation on the number of arguments.

Function definition example

Function func1 (p1, p2) {
    return p1 + p2;
}

Caller

var a1 = 2;
var a2 = 3;
func1 (a1, a2);

This example is a function that takes two arguments and returns a sum.

The following naming conventions apply to function names, which are the same as object names.

1. Beginning with English or Japanese characters
2. The second and subsequent characters must be alphanumerical, underscore (_) or Japanese.
3. The total length is within 30 bytes

Functions can be identified as part of an object tree starting with // , just like objects. For example, if you write as follows, you can call it as //.Form1.Button1.Func1 () .

 Form Form1 {
    :
    Button Button1 {
        :
        Function func1() {
            return 10;
        }
    }
}

If an object or function with the same name is already defined in the same hierarchical position, the previously defined object or function will be deleted and replaced with the later defined function.

A function can have any number of formal parameters, no more than zero . There is no upper limit. The function definition and caller do not necessarily have the same number of arguments. If the caller passes fewer arguments than defined, null values ​​are passed for subsequent formal arguments. Also, if you pass more arguments than the definition, the arguments that exceed the definition will be ignored.

It is not possible to define a variable number of arguments. When a variable number of arguments is required, define the maximum number of arguments that can be assumed as described above, and judge the actually passed argument by comparing with the null value, or use Array or array object.

Functions can return a return value with a return statement, but it is not required. If the return statement does not return a return value, the return value of the function is undefined and no meaningful result is returned.