1. The function name and the form parameter list are collectively referred to as "function signature".
The compiler determines which function to call based on the function signature. Therefore, although the function names are the same but the parameters are different, the compiler can always accurately locate which function is called.
Therefore, a program has two functions with the same name and parameters, but different return values. The compiler will not distinguish these two functions. It always considers the second as the most defined function, and an error will occur.
2. the return value of a function disappears at the end of the function, but its copy is returned to the function call.
For functions of the void return type, only return; is required.
Note: The right curly braces at the end of the function body. When the program is executed here, the right curly braces are equivalent to "return;". Therefore, the return value is void, even if there is no explicit return; no errors,
However, if there is a return value but no return, an error occurs.
3. functions can be used only after they are declared. When declaring a function, you only need to declare its type, including its form parameter. (For standardized writing, we should generally write it completely so that it is easy to read)
Double sum (double, double); // This is also the correct statement, only the type must be described.
Therefore, when an array is transmitted, the upper limit is not required. For example
Double sum (double A [], double B); // This is correct. do not include a value.
Double sum (double A [4], double B); // This is also true, with a value without changing its "type"
Double sum (double * P, double B); // This is correct because its type is consistent.
Further, because the array name can replace the pointer, but it is of the const type, the last one above can change the P value, which can be slightly enhanced:
Double sum (double * const P, double B); // It indicates that p is a constant and cannot be changed, but the data in the original array can be changed, for example, * P = 34;
Double sum (const double * P; Double B); // indicates that p is a variable, but the data in the original number group should be "constant". The data in the original number group should not be changed.
Further, since the function always uses a copy, it generally does not make much sense to regard the transferred copy as a const, just to differentiate reading. However, the data changes referred to by the replicas are of great significance.
4. Important: References
I. Definition:
Reference: the alias of a variable. Just like other people, there are also big names (variables) and small names (aliases ).
A variable can have several references, that is, several aliases. For example, when my parents call you "Zhang Wen", when I was a child, the village called you "dog left". After work, my colleagues called you "man" (BRA). It's all about you.
Int A = 3;
Int & B =;
Int & C = A; // The Second alias.
Reference. It must be initialized during definition to specify the alias.
Difference from pointer: References often look like pointers, but they can directly use references to replace the original variables. However, pointers do not work and must be removed, for example, * P = 3.
The other is that the pointer can be null, P = 0, but cannot be referenced (according to the initialization principle). It must be a fixed variable (this variable exists ).
Ii. Use references for function calls.
Whether it is a value or a pointer, it is passed to the function copy.
But the reference does not. It is omitted for directly transferring copies and uses the original value directly. (Therefore, high efficiency also brings the risk of modifying the original value)
Because the referenced and referenced variables are equivalent.
Double sum (double & A, double B); // here, a is the reference (alias) of the original variable, not a copy.
According to the definition, the reference is the alias of the "variable". Therefore, if you use an alias instead of a variable in the call function, an error will occur. For example, if the real parameter is a constant, the "type" is inconsistent. Therefore, the constant should be:
Double sum (double & A, const double & B); // note that A is the alias of the variable at the call, and B is the constant of the original call.
Sum (C, 3.2); // original call usage.
5. Main function parameters:
int main(int argc, char *argv[])
The main function has two parameters. The parameter service is followed by the command.
Parameter 1: argc indicates that there are several strings, and its pointer is in the array of the following parameters.
Parameter 2: argv [] a pointer array. The number of pointers is argc + 1. Because the last pointer must be 0, 1 is added. The first pointer must be a string pointing to the program name.
For example, DIR/W. This is the list of files in the doscommand.
Here, argc = 2. Because there are two strings, two pointers are required. That is, the first string is the program's own string: Dir, and the second string is "/W"
The argv [] pointer array has three elements (pointers). Besides the addresses (pointers) of the first two strings, the last element (pointer) is 0.
6. Default Value of the function.
In the function declaration, you can directly assign a value to the parameter with =, indicating the "default value" of the parameter ".
Important Principle: The parameters of the default value can only be arranged from the right, and ignored during the call is also omitted from the right. Skip is not allowed.
For example, assume that the declared function has five form parameters, and the third (right) has the default value. So when calling:
If one parameter is omitted, only 5th parameters are allowed.
Two parameters are omitted: 4th and 5, not 3rd or 5. (This is a jump. You can only step from right to left)
Number of dynamic parameters: 3rd, 4, and 5.
7. the return value is a pointer and reference. Note: Do not return a pointer or pointer to a local variable in the function.
As soon as the function is removed, its scope becomes invalid, and its pointer and reference become invalid, making it prone to errors.
In the main function example: * larger (a, B) = 100; // The original value is: return the pointer of the maximum value and change the maximum value to 100.
Sub-function: int * larger (int A, int B)
{If (A> B) Return &;
If (B> A) Return & B;
}
The above error occurs. Because A and B are copies, the address of the copy is returned. However, when the function is left, its variables will die.
Int * larger (Int & A, Int & B) is correct, because it is a reference, which is the main function. The scope will not expire.
Note that the new memory in the sub-function will still exist after the sub-function ends if it is not deleted in the sub-function.