Learning how to get started with as3-function (4): formal parameters and actual parameters

Source: Internet
Author: User

Function parameters are the internal communication methods between the outside and the function. For a function with parameters, the function has a data transfer relationship during function calling.

Check the Code:

// Call a function
Max (5, 7 );
// Define a function
Function max (X: int, Y: INT): int
{
If (x> Y)
{
X = X;
} Else {
X = y;
}
Return X;
}

If a function has a return value, you can use the Return Statement and declare the type of the return value when defining the function.

In the above Code, data is transmitted during function calling. 5 and 7 are first transmitted to X and Y, and then 7 is returned after comparison, passed to the function max (), so the value of max (5, 7) is 7.

When defining a function, the variables in brackets are called formal parameters ). The X and Y values of the max () function are the form parameters. When a function is called, the variables or expressions in the brackets following the function name are called actual parameters (real parameters for short ). For example, 5 and 7 when calling the max () function.

The real parameters of a function can be variables or expressions of any data type.

Parameters are very important for functions. A good function should have fewer parameters. For example, the tofixed () function can retain a multiple of the decimal points, but it returns a string. Below is a function that returns a number with a decimal point retained:

 

VaR A: Number = 2.55555;
// Retain two decimal places and Output
Trace (retain decimal point (A, 2 );
// Define a function
Function reserved decimal point (A: Number, B: uint): Number
{
// 10 to the power of B
VaR T: uint = math. Pow (10, B );
// After a * t is rounded up, Division T is returned.
Return math. Round (A * T)/T;
}

 

The decimal point retention function requires two parameters, one representing the number of decimal places to be retained, and the other being the number of decimal places to be retained.

Many functions of the form parameters can be replaced by objects. The following function calculates the distance between two points. Object P1 represents the coordinates of the first point, and P2 represents the coordinates of the second point:

 

// Call the function and assign the returned value to variable d
// Define the attributes of two objects during the call
VaR D: numbet = distance ({X: 100, Y: 200}, {X: 100, Y: 100 });
// Output distance
Trace (d );
Function distace (P1: object, P2: Object): Number
{
VaR X: Number = p1.x-p2.x;
Var y: Number = p1.y-p2.y;
VaR DIS: Number = math. SQRT (x * x + y * y );
Return DIS;
}

 

The definition object is generally in the form of "new object ()", but you can also use the "{}" operator to define the object, such as: "{X: 100, Y: 200} "is equivalent to the following code:

 

VaR obj1: Object = new object ();
Obj1.x = 100;
Obj1.y= 200;

 

Pass in parameters by value or by reference

Function parameters can be variables of any data type. data types include simple data types and complex data types.

If the parameter object is passed in by value, a copy of the parameter object will be copied for internal operation of the function, and the parameter itself will not be affected. If the parameter object is passed in by reference, the copy will not be copied, changes the status of the parameter object.

In as3, all parameters are passed in by reference. However, the simple data type is a constant object, and the effect of passing a reference is the same as that of passing a value. Therefore, if the parameter is a simple data type, it can be regarded as passing a value. If the parameter is a complex data type, that is, transfer reference.

For parameters of the simple data type, when a function call starts, a value is passed from the real parameter to the form parameter. When the function call ends, the form parameter is not passed to the real parameter, this type of data transfer is called value transfer.

Take a look at the following test procedure: See example: Value Transfer
VaR A: Int = 1;
Trace ("real parameter A =" + a) before function call );
Test ();
Trace ("after the function is called, the real parameter A =" + );
Function Test (x): void
{
X + = 5;
Trace ("calling a function, x =" + x );
}
Output result:
Before a function is called, the real parameter A = 1
In the call function, the parameter x = 6
After the function is called, the real parameter A = 1

From the test results, when a function is called, the value of the form parameter starts with the value 1 passed by the real parameter, and is changed to 6 after 5. However, the value of the form parameter is not passed to the real parameter after an example, therefore, the values of real parameters before and after function calls are 1.

At the beginning of a function call, data is transmitted from the real parameter to the form parameter. At the end of the function call, the form parameter is also transferred to the real parameter for data transfer.

Take a look at the following test procedure: See example: Upload reference
// Create an object
VaR person: Object = new object ();
// Dynamically create a Property Age
Person. Age = 20;
Trace ("person. Age =" + person. Age );
Test (person );
Trace ("person. Age =" + person. Age );
Function Test (PER: Object): void
{
Per. Age = 10;
// Modify the age attribute of an object
Trace ("function call, parameter per. Age =" + per. Age );
}
Test results:

Before the function is called, the real parameter person. Age = 20
In function call, the parameter per. Age = 10
After the function is called, the real parameter person. Age = 10

The result shows that the value of the real parameter changes before and after the function is called. After the real parameter passes the value to the form parameter, the modified value is passed to the real parameter.

In flash, objects, arrays, and other complex data type variables transmit data by reference.

Default Value of the Parameter

As3 has two new features: You can specify a default value for a parameter when defining a parameter, and you can specify parameters with uncertain function.

When calling a function, if the number of the given actual parameters does not match the number of formal parameters, an error is displayed, indicating that the number of parameters does not match.

For example, the following code has two parameters, and the real parameter has only one

 

// Error: 1136: the number of parameters is incorrect. It should be two
Test (1 );
Function Test (X: int, Y: INT): Boolean
{
Return x> Y;
}

 

The default parameter function can be used here. The default parameter format is as follows:
Function Name (parameter: data type = default value): Data Type
{
}

The following is the correct code:

 

Test (1 );
Function Test (X: int, Y: Int = 0): Boolean
{
Return x> Y;
}

 

When calling a function, only one parameter is passed, but since the second parameter has a default value of 0, the function is actually called as follows:

Test (1, 0 );

Any number of parameters

In the trace () function, one or more parameters of any data type can be passed to this function.

Trace (parameter 1 );
Trace (parameter 1, parameter 2, parameter 3 );

This variable can be implemented in as3. as3 allows the function to set any number of parameters:

Function Name (... parameter): Data Type
{
}

"..." In "..." represents any number. The parameter name can be any valid variable name.

Use of any number of parameters:

// Call a function
Test ();
// Define any number of parameters
Function Test (... Arg): void
{
Trace (Arg is array );
}

Any number of parameters means that there can be 0 to multiple parameters. The output result is true.

The following code passes three parameters:

 

Test (1, 2, 3 );
Function Test (... Arg): void
{
Trace (ARG );
}

 

The output result is 1, 2, 3, that is, all elements in the array Arg are output.

Note the following when using any number of parameters: If the function has multiple parameters, any number of parameters must be written at the end; otherwise, the program reports an error.

Learning how to get started with as3-function (4): formal parameters and actual parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.