Formal parameters and actual parameters of functions

Source: Internet
Author: User
Tags new features

The parameter of function is the method of communication between outside and function, and for the parameter function, the function has the relation of data transfer.

Look at the code:

Call function
Max (5,7);
Defining functions
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 return statements and declare the type of the returned value when you define the function.

The above code takes place during the function call, passing 5 and 7 to X and Y, then returning a large value of 7 by comparison and passing to the function max (), so the value of Max (5,7) is 7.

When you define a function, the variable in parentheses is called the formal argument (the parameter). The X and Y that define the max () function are the formal parameters. When a function is called, the variable or expression in parentheses after the function name is called the actual argument (the argument, for short). such as 5 and 7 when the Max () function is invoked.

A function's argument can be a variable or an expression of any data type.

Parameters are very important for a function, and a good function should have no more than a few arguments. For example, the toFixed () function can retain a multiple of the decimal point, but it returns a string. Write a function that returns the decimal point that the value is a number:

var a:number = 2.55555;
Keep two decimal digits and output
Trace (keep decimal point (a,2);
Defining functions
function reserved decimal point (A:number,b:uint): number
{
10 B-Square
var t:uint = Math.pow (10,b);
A*t after rounding T and returning
Return Math.Round (a*t)/t;
}

The function that retains the decimal point requires two parameters, one representing the value to keep the decimal point, and the number of digits to keep the decimal point.

For a lot of parameters of the function, can be replaced by objects, the following function to find the distance between two points, with the object P1 represent the coordinates of the first point, with P2 representing the 2nd coordinates:

Call the function and assign the return value to the variable D
Define properties for two objects when called
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 generally has the form "new object ()", but it can also be defined by the "{}" operator, 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

The parameter of a function can be a variable of any data type, and the data type is divided into simple data types and complex data types.

Passed in by value, the parameter object copies a copy for internal operation of the function, the parameter itself is not affected, passed by reference, the copy is not replicated, the reference to the action parameter object in the function changes the state of the Parameter object.

In as 3, all parameters are passed in by reference. Simply, the simple data type is invariant, and the effect of passing references and values is the same, so if the parameter is a simple data type, it can be thought of as a pass value, if the parameter is a complex data type, or a reference.

For parameters of a simple data type, the function call begins with a value pass from the actual parameter to the parameter, and the parameter is not passed to the argument at the end of the function call, which is called a value pass.

Look at the following test program: see Example: Value delivery
var a:int = 1;
Trace ("Before the function call, the argument a=" +a);
Test (a);
Trace ("Function call, Argument a=" +a);
function test (x): void
{
x + 5;
Trace ("Call function, formal parameter x=" +x);
}
The output results are:
Argument A = 1 before the function is called
In the calling function, the formal parameter x = 6
Argument A = 1 after the function call

Judging from the test results, when calling a function, the value of the formal parameter begins with the value 1 passed by the argument, plus 5 to 6, but the value of the formal parameter is not passed to the argument, so the value of the arguments before and after the function call is 1.

When a function call begins, data is passed from the argument to the parameter, and the parameter is passed to the argument at the end of the function call, which is referred to as a reference pass.

Look at the following test procedure: see example: Pass a reference
Create an Object
var person:object = new Object ();
Dynamically Create attribute Age
Person.age = 20;
Trace ("Before the function call, the argument person.age=" +person.age);
Test (person);
Trace ("Function call, Argument person.age=" +person.age);
function test (per:object): void
{
Per.age = 10;
Modify the Age property of an object
Trace (in "function call, Parameter per.age=" +per.age);
}
Test results are:

Argument person.age=20 before a function call
In a function call, the formal parameter per.age=10
After the function call, the argument person.age=10

The result shows that the value of the argument changes before and after the call function. When the argument passes the value to the formal parameter, the formal parameter passes the changed value to the argument.

Complex data type variables such as objects and arrays in Flash pass data in a reference way.

Default values for parameters

AS3 has two new features that give the parameter a default value when you define the parameter, and you can give the function an indeterminate parameter.

When a function is invoked, if the given actual parameter does not match the number of formal arguments, an error indicating that the number of arguments is mismatched is indicated.

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

Error: 1136: The number of parameters is incorrect and should be two
Test (1);
function test (x:int,y:int): Boolean
{
Return x>y;
}

The parameter default value function is available here, and the default value of the parameter is defined in the form:
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 a function is invoked, only one argument is passed, but because the second parameter has a default value of 0, the function is actually called:

Test (1,0);

Any number of parameters

You can pass a parameter of one or more arbitrary data types to this function in the trace () function.

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

This indefinite number of parameters, which can be implemented in AS3, AS3 allow any number of parameters to be set for the function:

function functions Name (...) Parameters): Data type
{
}

"... Parameter "..." represents any number, and the parameter name can be any valid variable name.

Use of any number of parameters:

Call function
Test ();
Functions that 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, as can be seen from the output true.

The following code passes 3 parameters:

Test (1,2,3);
function test (... arg): void
{
Trace (ARG);
}

The output is 1,2,3, which outputs all the elements in the array arg.

Use any number of parameters to be aware of: if the function has more than one parameter, any number of parameters must be written at the end, otherwise the program person error.

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.