Return Value
The system automatically generates a copy of the returned value, which can be obtained from the return point in the program.
Function prototype Declaration
Unless the function is defined before the same source file, the function prototype Declaration must be used (usually after # include and using ):
# Include <iostream>
Using namespace STD;
Double Power (Double X, int y); // function prototype declaration, the form parameter can be different from the function implementation, or even write only double, int
Int main ()
{
X = power (Y, Z );
}
Double Power (double X1, int Y1) // function implementation
{}
Two forms of function passing real parameters:
1. the parameter is a common variable (not a reference)
1. Value-based transfer mechanism
The real parameter is not passed to the function at all, but a copy of the real parameter with the same value is passed to the function, so the execution of the function does not affect the real parameter:
Int x = 1;
Int change (X );
Int X is not changed regardless of the change function.
2. Pass pointer arguments to the Function
The passed pointer stores a copy of the pointer with the same address as the real parameter pointer. Because the pointing address is the same, you can change the pointed address.
Example:
Void change (int *);
Int main ()
{
Int x = 10;
Int * PX = & X;
Change (PX );
Cout <* PX <Endl;
}
Void change (int * px)
{
* Px + = 10;
}
Result:
10
3. Pass the array to the Function
In essence, it is to pass pointer and pointer copy to the function (the compiler converts the array name to a pointer) instead of an array copy (copying an array needs to be very large-sell)
Void change (INT [], INT );
Int main ()
{
Int X [] = {1, 2, 3, 4, 5 };
Int COUNT = sizeof x/sizeof X [0];
Change (x, count );
For (INT I = 0; I <count; I ++)
{
Cout <X [I] <"";
}
}
// Void change (INT arrayx [], int count)
Void change (int * arrayx, int count) // It is equivalent
{
For (INT I = 0; I <count; I ++)
{
Arrayx [I] + = 1;
}
}
Result: 2 3 4 5 6
4. Multi-dimensional array
The multi-dimensional array of local C ++ is essentially an array, so it is also a pointer copy (for example, a two-dimensional array transmits a pointer copy pointing to the first line address)
Example:
Void change (INT [] [4], INT );
Int main ()
{
Int X [] [4] = {1, 2, 3, 4}, {5, 6, 7, 8 }};
Int COUNT = sizeof x/sizeof X [0];
Change (x, count );
For (INT I = 0; I <count; I ++)
{
For (Int J = 0; j <4; j ++)
{
Cout <X [I] [J] <"";
}
Cout <Endl;
}
}
Void change (INT arrayx [] [4], int count)
// You can also write it as void change (INT (* arrayx) [4], int count) and then use ++ * (arrayx + I) + J, the function prototype declaration should also be changed to the corresponding form.
{
For (INT I = 0; I <count; I ++)
{
For (Int J = 0; j <4; j ++)
{
++ Arrayx [I] [J];
}
}
}
Result:
2 3 4 5
6 7 8 9
2. Pass reference parameters to the Function
The parameter is the alias of the real parameter, which eliminates the copy of the real parameter and allows the function to directly access the real parameter in the called function.
It is useful when using class objects, because copying large class objects takes a lot of time.
Example:
Void change (Int &);
Int main ()
{
Int x = 10;
Change (X );
Int y = 20;
Change (y );
Cout <x <"" <Y <Endl;
}
Void change (Int & X)
{
Cout <"receive" <x <Endl; // outputs a value rather than an address.
X ++;
}
Result:
Receive10
Receive20
11 21
Note:
Reference basic attributes: After declaring and initializing the reference, you cannot reassign it to another variable. However, a new reference is initialized for the execution of the callback function.
This article is from the "flyclc" blog, please be sure to keep this source http://flyclc.blog.51cto.com/1385758/1540150