If you want a function to return a value, you must use the return statement.
In addition to output information, the following functions return output information. In this case, the return type of the function changes from the void type to the "*" type:
VaR S: String = trace ("hello "); Function tracemsg (MSG :*) { Trace (MSG ); Return MSG; } |
The return statement can interrupt the execution of the function, which is often used in the judgment statement. If a condition is false, the code is returned without executing the code.
The following code checks whether a function parameter is a number. If it is not a number, the return statement is used to directly return the result without executing the following code:
Function Area (r: *): void { VaR B: Boolean = R is number; If (! B) Return; Trace ("subsequent Code "); } |
The function defined here uses R as a parameter. In the function, first judge whether the parameter is a number. If it is not a number, exit the function directly using the return statement, and the subsequent code will not be executed.
Compare the following function call results. The former can execute the output statements in the function, and the latter cannot:
Area (7 );
Area ("7 ");
In some functions, you need to write multiple return statements. For example, in a condition statement, each condition branch can correspond to a return statement.
The following method returns different instances based on parameters:
Function Factory (OBJ: string): Load { If (OBJ = "XML "){ Trace ("Return loadxml instance "); Return new loadxml; } Else if (OBJ = "sound "){ Trace ("Return loadsound instance "); Return new loadsound; } Else if (OBJ = "movie "){ Trace ("Return loadmovie instance "); Return new loadmovie; } Else { Trace ("error "); } } |
How to compile Functions
We compile a function example to illustrate the function compilation process.
First, determine the function parameters. Because the point is expressed in two-dimensional coordinates in flash, two parameters are required for each point, and four functions are required for finding the distance function between two points, since coordinates and distances are both represented by numbers, the parameter type is number.
The initial framework of the function:
Function distance (X1: Number, Y1: Number, X2: Number, Y2: Number): Number { Return 0; } |
The return statement that returns 0 is added to the function. This Return Statement is required because no error occurs during the test of the program only when a value of the number type is returned.
To test whether the function is correct, you only need to call the function.
VaR DIS: Number = distance (0, 0, 100,100 );
Trace (DIS );
If the function definition is correct, you can add code step by step in the function. For complex functions, each code added must be tested in a timely manner, and errors can be corrected in a timely manner.
Add code to the function:
Function distance (X1: Number, Y1: Number, X2: Number, Y2: Number): Number { VaR X: Number = x2-x1; Var y: Number = Y2-Y1; Trace ("x =" + X, "Y =" + y ); Return 0; } |
The difference between x and y is added to the function above, and the trace () function is added to debug the result.
After the function is called, the values of X and Y are displayed in the output panel. the correctness of the values of X and Y can be determined through simple calculation.
After confirming that the trace () function is correct, comment out the trace () function.
Finally, add the code to the function to calculate the distance:
Function distance (X1: Number, Y1: Number, X2: Number, Y2: Number): Number { VaR X: Number = x2-x1; Var y: Number = Y2-Y1; // Trace ("x =" + X, "Y =" + y ); VaR DIS: Number = math. SQRT (x * x + y * y ); Return DIS; } |
After the above function is called, the distance between two points can be output. However, it is difficult to manually calculate the distance from a vertex (100,100) to a vertex (). Therefore, it cannot be ensured that the obtained distance is correct.
At this time, it is best to output two points that know the distance to check whether the result is correct. According to the common sense of the hooks, the following coordinates can be input in the function for calculation:
VaR DIS: Number = distance (0, 0, 3, 4 ); Trace (DIS ); |
If the output result is 5, the distance function is compiled.
Learning how to get started with as3-function (2): Return Statement of Function