In the previous article, I wrote about the expansion of C ++ comments, input and output, and local variable descriptions, as well as the comparison between the const modifier and # define in C, I have also received help from several C ++ friends. Thank you very much. I also hope to welcome more friends who have learned C ++ to discuss this together, so that everyone can make common progress. So today we will talk about the differences between C ++ and C in the function prototype, inline functions, functions with default parameters, and function overloading.
1. Everyone is familiar with C. If the function call location is prior to the Function Definition, you should define the function directly before declaring or calling the function prototype. For example:
# Include <stdio. h>
Int add (int x, int y );
Int main ()
{
Int X, Y, sum;
Printf ("enter two integers: \ n ");
Scanf ("% d, % d", & X, & Y );
Sum = add (x, y );
Printf ("x + y = % d", sum );
Return 0;
}
Int add (int x, int y)
{
Return X + Y;
}
However, you can also declare it in a concise way, for example, int add (); all can be compiled. However, in C ++, if the function is defined after, before the call, the declaration of the function prototype must be int add (int x, int y). The function name, parameter type, number, and return value must be described. If the function definition is before, the call is the same as that of C. The above form is also equivalent to int add (INT, INT) in C ++. If the return type C ++ is not specified in the prototype description, the default return type is int, void is used if no return value is required. In addition, the return value of the main function required by the Standard C ++ must be int;
2. inline functions are prefixed with the keyword "inline" before function description. When C ++ is compiled, the code in the function body is inserted into the statement where the function is to be called, at the same time, real parameters are used to replace the form parameters so that function calls are no longer performed when the program is running. For example:
# Include <iostream>
Using namespace STD;
Inline int add (int A, int B)
{
Return A + B;
}
Int main ()
{
Int X, Y, sum;
Cin> x> Y;
Sum = add (x, y );
Cout <"x + y =" <sum <Endl;
Return 0;
}
During compilation, when a function Ah add (X, Y) is encountered, replace add (x, y) with the function body, and replace the form parameter with the real parameter, so that "sum = add (x, y, y) "replaced with" {
Int A = x; int B = y; sum = a + B;} "; why should I introduce an inline function? The main purpose is to eliminate the system overhead during function calls to increase the system running speed. When a function is called during program execution, the system stores some current state information of the program in the stack and forwards it to the code of the function to execute the Statement of the function body, these parameters require time and space overhead for saving and passing, reducing program efficiency. However, not all functions can be defined as inline functions. Generally, only small functions are defined as inline functions with frequent functions, which greatly improves the running rate.
3. generally, the number of real parameters should be the same as that of the form parameter, but not necessarily in C ++. The method is to specify the default value for one or more form parameters when describing the function prototype, call this function later. For example, if one of the real parameters is omitted, C ++ automatically uses the default value as the value of the corresponding parameter. For example, if int add (INT x = 10, int y = 10) is used, we can call this function in three ways: add (50, 50) // The result is 50 + 50; add (50) // The result is 50 + 10; add () // The result is 10 + 10; this makes the function more flexible. Note that the default parameter must be at the rightmost end of the parameter list. Int add (int x, int y = 10, int z) is incorrect, if a parameter cannot be omitted, specify the parameter value for the subsequent parameter. If the function is defined after the function is called, the function declaration is required before the function is called. The default value must be provided in the function declaration, do not give the default value when defining the function (because some C ++ compilation systems will give the error message "specify the default value repeatedly );
4. function Overloading is a must-have for anyone who wants to learn C #, it means that as long as the type of function parameters is different, the number of parameters is different, or both of them exist, two or more functions can use the same function name. Despite being simple, I still want to talk about the following issues in C ++: 1. the function return value is no longer a column of the function parameter matching check; 2. function overload and use with default parameters may cause ambiguity, for example, int fun (INT x = 0; int y = 10) {return X + Y ;} and int fun (int r) {return r;} at this time I call fun (10); 3. if the real parameters provided by the function call do not match the type of the parameter, C ++ will customize the type conversion and continue to execute the conversion successfully. However, in this case, an unrecognized error may occur: int add (int x, int y) and long add (Long, long). In this case, I call add (9.9, 8.8 );
5. At last, let's summarize the content of today through an instance:
1 # include "stdafx. H"
2 # include <iostream>
3 usingnamespace STD;
4
5 Int add (int x, int y); // or Int add (INT, INT)
6
7 inline int sub (int x, int y) // Inline Function
8 {
9 return x-y;
10}
11
12 double MUL (Double X = 10.0, Double Y = 10.0); // a function with default parameters
13
14 float add (float X, float y) // function overload
15 {
16 return X + Y;
17}
18
19 int main ()
20 {
21 int X, Y, result;
22 cout <"enter two integers :";
23 CIN> x> Y;
24 result = add (x, y );
25 cout <"common function (addition): x + y =" <result <Endl;
26
27 cout <"enter two integers :";
28 CIN> x> Y;
29 result = sub (x, y );
30 cout <"inline function (subtraction): x-y =" <result <Endl;
31
32 double A, B, mul_result;
33 cout <"enter two double precision numbers :";
34 CIN> A> B;
35 mul_result = MUL (A, B );
36 cout <"function with default parameters (multiplication): a * B =" <mul_result <Endl;
37
38 float C, D, sum;
39 cout <"enter two single precision numbers :";
40 CIN> C> D;
41 sum = add (c, d );
42 cout <"overload addition function: C + D =" <sum <Endl;
43
44 return0;
45}
46
47 int add (int x, int y)
48 {
49 return X + Y;
50}
51
52 double MUL (Double X, Double Y)
53 {
54 return x * Y;
55}
Result: