The previous essay wrote about C + + in comments, input and output, local variable description of the expansion, and the Const modifier and C in the comparison of # define, also got a few learning C + + friends help Explain, very grateful, I also hope to welcome more friends to learn C + + to discuss together, So that everyone can progress together. So, today this is about C + + on function prototypes and C, inline functions, functions with default parameters, and overloads of functions.
1. Everyone is familiar with C, if the function calls the position before the function definition, then before the function call to declare or call the function before it is directly defined. Like what:
#include <stdio.h>
int add (int x,int y);
int main ()
{
int x,y,sum;
printf ("Please 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, it can be declared in a concise way, such as: int add (); Add (); Can be compiled, but in C + +, if the function is defined in the previous call, the Declaration of the function prototype must be an int add (int x,int y), the function name, the type and number of arguments, and the return value must be stated, if the function is defined before, the call is the same as C. The above form is also equivalent to int add (int, int) in C + +, if the return type C + + is not indicated in the prototype description the default return type is int, no return value is required, and void is used. In addition, the standard C + + requires that the return value of the main function must be int;
2. Inline functions are prefixed with the keyword "inline" before the function description, and when C + + is compiled with code in the body of the function inserted into the statement where the function is to be called, the arguments are substituted for the formal parameters so that the function calls are no longer made while the program is running. Like what:
#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;
}
At compile time, when the function, add (x, y) is encountered, the function body is substituted for add (x, y), and the argument is substituted for the parameter, so that "Sum=add (x, y)" is replaced with "{
int a=x;int b=y;sum=a+b;} "; So why do we introduce inline functions? It is mainly to eliminate the overhead of the function call to improve the speed of the system. During program execution, the system will save some current state information of the program to the stack, and go to the code of the function to execute the statement of the function body, which takes time and space overhead in saving and transferring, which makes the program less efficient. But not all functions can be defined as inline functions, in general, only a small size is defined as an inline function with frequent functions, which can greatly improve the operating rate.
3. In general, the number of arguments should be the same as the formal parameters, but not necessarily in C + +, the method is to explain the function prototype, the default value for one or more parameters, and later call this function, such as omitting one of the arguments, C + + automatically with the default value as the corresponding parameter. such as int add (int x=10,int y=10), then we can call this function to have three kinds of writing: Add (50,50)//Results for 50+50;add (50)//Results for 50+10;add ()//results 10+10; This makes the function more flexible. Note, however, that the default parameter must be at the very right end of the parameter list, int add (int x,int y=10,int z) to be wrong, and not allow a parameter to be omitted, and then specify the parameter value for the argument followed. If a function is defined after a function call, a function declaration is required before the function call, at which point the default value must be given in the function declaration, and no default value is given when the function is defined (because some C + + compilation system will give the error message "Duplicate specified default value");
4. Overloading of functions, for this I want to learn C # friends must be familiar with, it means that as long as the function parameters of different types, or the number of arguments, or both, two or more of the functions can use the same function name. Although simple, I would like to talk about it in C + + to pay attention to a few problems: 1. Function return value is no longer the function parameter matching check; 2. function overloading with functions with default parameters can cause ambiguity such as: int fun (int x=0;int y=10) {return x+y ;} and int fun (int r) {return r;} This time I call fun (10); 3. If the arguments and formal parameter types given by the function call do not match, C + + will customize the execution type conversion, and the conversion will continue to execute, but in this case an unrecognized error may occur: int add (int x,int y) and long Add ( Long,long), this time I call Add (9.9,8.8);
5. Finally, let's summarize the content of today through an example:
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
Double Mul (double x=10.0,double y=10.0);//function with default arguments
13
Float Add (float x,float y)//function overload
15 {
X+y return;
17}
18
int main ()
20 {
int X,y,result;
cout<< "Please enter two integers:";
cin>>x>>y;
Result=add (x, y);
cout<< "normal Function (addition): x+y=" <<result<<endl;
26
cout<< "Please enter two integers:";
cin>>x>>y;
Result=sub (x, y);
cout<< "inline function (subtraction): x-y=" <<result<<endl;
31
Double A,b,mul_result;
cout<< "Please enter two double-precision number:";
cin>>a>>b;
Mul_result=mul (A, b);
cout<< "Functions with default parameters (multiplication): a*b=" <<mul_result<<endl;
37
C,d,sum float;
cout<< "Please enter two single-precision number:";
cin>>c>>d;
Sum=add (C,D);
cout<< "Overloaded addition function: c+d=" <<sum<<endl;
43
Return0;
45}
46
int add (int x,int y)
48 {
X+y return;
50}
51
Mul double (double x,double y)
53 {
X*y return;
55}
Results:
Non-object-oriented features of C + + extensions (2)