one, inline function
Calling a function requires a certain amount of time and space overhead. Function Call Procedure:
C + + provides a way to improve efficiency by embedding the code of the called function directly into the keynote function at compile time, rather than turning the process out. This function, embedded in the keynote, is called an inline function.
#include<iostream>using namespace std;inline int sum(int a,int b);int main(){int a=100;int b=200;cout<<sum(a,b)<<endl;return 0;}inline int sum(int a,int b){return a+b;}
When you call sum (A, b), replace sum (A, b) with the code of the SUM function body.
Note that you can write inline at the same time as you declare a function and define a function, or you can declare inline in one of the same places.
For: Simple functions that are small and frequently called.
Second, overloading of functions
When programming, sometimes we want to implement the same class of functions, but some of the details are different. such as the sum () function:
int sum1(int a,int b);int sum2(int a,int b,int c);double sum3(double a,double b);float sum4(float a,float b);
C + + allows multiple functions to be defined with the same function name, which can have different parameter types and parameters.
int sum(int a,int b);int sum(int a,int b,int c);double sum(double a,double b);float sum(float a,float b);
Third, function template
The so-called function template, is actually to establish a general function, its function type and formal parameter type is not specified, with a virtual type to replace. Functions with the same function body can be replaced with this template, do not have to define multiple functions, just one in the template can be defined. When a function is called, the system replaces the virtual type in the template based on the type of the argument.
template<typename T>T sum(T a,T b,T c){return a+b+c;}
Defines the general form of a function template:
Template<typename t> or Template<class t>
Type parameters can be more than one:
Template<typename T1,class t2>
Iv. functions with default parameters
In general, when a function is called, the parameter gets the value from the argument. Sometimes when the same function is called multiple times with the same argument, C + + provides a simple way to deal with a default value for the parameter, so that the parameter does not have to be the actual parameter value.
flaot area(float r=6.5);area(); //相当于area(6.5);
V. Nested calls to functions
C + + does not allow nested definitions of functions, that is, one function cannot completely contain another function. Each function in a program is defined in parallel and independent of each other. Although C + + cannot nest definition functions, you can nest call functions.
Vi. recursive invocation of functions
The function itself is called indirectly or directly in the process of invoking a function, called a recursive call to a function.
Vii. internal functions and external functions
intrinsic function: can only be called by other functions in this file
External functions: defaults to external functions, which can be called by other files
static int fun(int a,int b);extern int fun(int a,int b);
Some features of C + + functions