6.1 Function Basics
Functions include: return type, function name, parameter list, function body
Function call: ① The initial taxiing parameter with arguments, ② control to the modulated function.
Return statement: ① Returns the value of the return statement; ② control is transferred to the keynote function.
Warming
The return type of a function cannot be an array type or a function type, but it can be a pointer to an array and a function.
An automatic object defined in the function body that is destroyed after the function ends.
A static object defined in the body of a function, defined only once in the program.
A function declaration is also called a function prototype, usually in a header file, without a function body.
6.2 Parameter passing
When the function is called, the parameters are recreated and the arguments are used to initialize the taxiing parameter.
Value passing and pointer passing are the data that corresponds to the actual parameter memory that is copied to the corresponding memory in the formal parameter.
Using reference passing is the binding of a parameter to the object that initializes it. (Personally, it is a handy notation for pointer passing, which is a syntactic sugar)
Noting
Use references as much as possible to avoid copying. If the arguments are not modified in the function, the const qualification should be used.
Using a reference, you can also return the contents of the function through parameters.
Array formal parameters
Array: ① does not allow copying; ② uses array names, which are usually converted to pointers.
//the following definitions are equivalent, and the compiler ignores array lengths//defined as const, cannot modify the array value by pointer, if need to modify, do not apply constvoidPrintConst int*);voidPirnt (Const int[]);voidPrintConst int[Ten]);//it is usually necessary to indicate the size of the array or end tag//if it is a C string, you can end it by the last bit of 'voidPrintConst int*beg,Const int*end);voidPrintConst intIa[],Constsize_t size);//array reference parameter, dimension is part of type//This can be done using sizeof to determine the number of bytes occupied by the array//This practice also limits the use of arrays of size 10 onlyvoidPrintint(&arr) [Ten]);
Multidimensional Arrays:
// The matrix points to the first element of the array, which is a pointer to 10 integers void print (int (*matrix) [rowsizeint ]; // The matrix actually points to the beginning of an array containing one-dimensional integers // 10 ignored by compiler void print (int matrix[][int rowsize);
Main function
// Both are equivalent int Main (intChar *argv[]); int Main (intchar **argv);
Functions of deformable parameters
Initializer_list parameter: The number of parameters is unknown and the type is the same.
// when defined void error_msg (Errcode E, initializer_list<string> msgs); // when called, it can be seen that there are actually two parameters, the second parameter uses the list error_msg (Errcode (0), {"function error" , "numbererror"});
Ellipsis parameter
Just to access the special C code, the code uses the VarArgs C standard library functionality. Most say type objects cannot be copied correctly when passed to an ellipsis parameter, so they are not normally used.
How to define: void foo (param_list, ...); For example, the printf function in C.
6.3 Return value of function
The function returns a copy after return.
Warming
Because all objects defined inside the function body are destroyed after the function ends, a pointer or reference to the local variable is returned, and the undefined condition occurs.
function returns the left value
Only the return value of a function is a reference, the value returned is the Lvalue, and the other is the right value.
You can assign a function result with a return type of a very left value.
// function Definition Char &get_val (string &str); // function uses ' A ';
Returns a list
vector<String> process () { //... return {str1, str2, STR3};}
Returns an array pointer or reference
Because arrays cannot be copied, you can only return pointers or references to arrays.
//①usingARRT =int[Ten];ARRT*func (inti);//②int(*func (inti)) [Ten];//③ tail return typeAuto Func (inti)int(*) [Ten];//④intodd[Ten]={};d Ecltype (odd)*func (inti);
6.4 Function overloading
Const formal Parameter
// top-level const cannot be distinguished Record Lookup (Phone); Record Lookup (const Phone); // The underlying const can distinguish between Record lookup (phone*); Record Lookup (const phone*);
Using Const_cast in Overloads
//If the passed argument is const, the return is also const//the passed arguments are not const, and return is not constConst string&shorterstring (Const string&S1,Const string&S2) { returnS1.size () <= s2.size ()?s1:s2;}string&shorterstring (string&S1,string&2) {Auto&r = shorterstring (const_cast<Const string&>(S1), Const_cast<Const string&>(S2)); returnconst_cast<string&>(R);}
6.5 Special Features
Default actual parameters
When a function is called, the parameter is initially replaced with the default argument, so the default argument can be assigned a value by a definite function.
inline functions
Inline
constexpr function
implicitly-inline.
is a function that can be used in a constant expression.
The function returned is not necessarily a constant, as in the case of a constant expression, the compiler will make an error.
6.7 Function pointers
BOOLLengthcompare (Const string&,Conststrng&);//declares a pointer to the function, uninitializedBOOL(*PF) (Const string&,Conststrng&);//Initialize the pointer,& optional, both of which are equivalentPF = Lengthcompare;//automatically converted into pointersPF = &Lengthcompare;//invoke with Pointer, * optionalBOOLB1 = PF ("Hello","world!");BOOLB1 = (*PF) ("Hello","world!");//using type aliases//function type, automatically converted to pointer when usedtypedefBOOLFunc (Const string&,Conststrng&); typedef decltype (Lengthcompare) Func; //pointer type to functiontypedefBOOL(*func) (Const string&,Conststrng&); typedef decltype (Lengthcompare)*Func;//The function pointer does the formal parameter, the three is equivalentvoidUsebigger (Const string&,Conststrng&, BOOL(*PF) (Const string&,Conststrng&));//pointer arguments are explicitly declaredvoidUsebigger (Const string&,Conststrng&, BOOLpfConst string&,Conststrng&));voidUsebigger (Const string&,Conststrng&, Func);//function that declares a pointer to a functionBOOL(*function (...)) (Const string&,Conststrng&); Func function (...); //using type aliasesAuto Function (...)BOOL(*) (Const string&,Conststrng&);//Tail TypeDecltype (lengthcompare) function (...);
6th Chapter function