5.2.2 Function Calling
A well-written function must be called legally before it can play its role in application. The function call will return a value, such as a calculation result. Functions in WML Script can be mainly divided into internal functions, external functions and library functions. Below we will introduce the calling methods of these three types of functions.
Internal functions
The so-called internal function refers to a function whose definition is in the same script file as the calling function. The call to the internal function is called an internal call. The call of an internal function is very simple. You only need to provide the function name and the required parameter value. The parameter value must be consistent with the number of parameters specified at the function definition, that is, the type. Moreover, function calls need to use operators to receive or process the called return value.
An internal function can be called before its definition or after its definition. For example, the following is an example of calling after the function definition.
function test1(val){
return val*val;
};
function test2(param){
return test1(param+1);
};
In this example, two functions test1 and test2 are defined. The test1 function is used to calculate the square of the given parameter value and return the result; the test2 function adds the given parameter value by 1, and then the sum is the parameter value to call the test1 function. After obtaining the result, the result will be returned to the statement calling the test2 function.
Note that in this example, the test2 function calls the test1 function. This method of calling other functions in the function is called function call nesting. WML Script's internal functions, external functions and library functions all support nested calls. We will introduce this content later.
External functions
External function makes a function defined in a WML Scrupt external file. The method of calling an external function is basically similar to the method of calling an internal function. The difference is that when the external function is called, one is to specify the address of the external file, that is, the name of the external file, and the other is to add the name of the external file before the name of the called external function.
WML Script rules use use url to specify external files, and the syntax format is:
use url and the external file name of the external function The URL where the external file resides;
In this way, the WML Script precompiled header can map external files into an identifier that can be used internally. Then, use this identifier and add the pound key (#) and standard function calls to implement external function calls, with the syntax format:
External file name #external function (parameter list);
For example, there is an external file we need under /script, called OtherScript, so we can use use url to specify the file:
use url OtherScript"/script"
This external file contains the external function testme that we need to call, so it can be called in the form of "external file name #external function (parameter list)":
OtherScript#testme(param+1);
This example is written in full, which is the following program:
use url OtherScript"/script"
function test(param){
return OtherScript#testme(param+1);
};
Previous page123456789Next pageRead the full text