C ++ Primer Plus Chapter VII function

Start function, similar to the python, nothing to say

7.1.2 function prototypes and function calls

Function prototype described interface function to the compiler, the type and number of parameters and the type of return value of a function it tells the compiler.

double cube(double x);
 
int main()
{
    double val = cube(5.0);
    return 0;
}
 
double cube(double x){
    return x * x;
}

First prototype tells the compiler, cube () has a double parameter. If the program does not provide this parameter, the prototype will allow the compiler to catch such errors. Secondly, after the end of the function call, the return value will be placed in the specified location. Then call the function from this position to obtain the return value. Since the function prototype indicates the function's return value is double type, so the compiler knows how many bytes to be retrieved and how to interpret them. Without this information, the compiler can only guess, but the compiler does not do so.

Function prototype function

Function prototypes can help the compiler do a lot of work, at the same time, it can also help reduce the chance of a great program program error. Prototyping can ensure the following:

1, the compiler handler correctly return values.

2, the number of parameters to retrieve the compiler used correctly.

3, parameter type compiler checks using the correct, if not correctly converted to the correct type.

int x = 5;
double val = cube(x);

See the above first function call, the program will be a value of type int 5 is transmitted to cube (). Compiler noted, Cube () specifies a prototype double type parameters, will convert 5.0 5

7.2 and passed by value function parameters

Mass participation would not spade. . . .

7.3 functions and arrays

Guess you like

Origin blog.csdn.net/u013693952/article/details/90726919