The pit!

5.1 parameter

int f(i++,i)
    // By default, it is not clear whether I or I ++ should be evaluated first. There is no specification for the order in which the arguments are evaluated, but we can assume that the arguments are evaluated from back to front
int f(int a,int)
    // The parameter name is optional. In general, a name is required. Otherwise, the parameter cannot be found
int f(a)
{
    static int a=0;
    // The lifetime of this local static object is up to the end of the process! The process!
    // Default to 0 if not explicitly initialized (built-in type)
}
int f(int ,int);
	// The parameter name is not required in the declaration.
	// The function has three elements: the return type, the function name, and the parameter type
int f(int &a,int &b)
    // Pass a reference, modifiable
	// but you cannot pass a literal, such as f(1,2), and a reference type cannot be initialized to a literal
int f(const int &a,const int &b)
    // This will do... F of 1,2 is fine
// Use references to avoid copying especially large strings!
// When you pass an array, you're actually passing a pointer to the array
// Multi-dimensional array
// If there is a problem, think about whether there is a problem with initializing the parameter this way
Copy the code

Ellipsis parameters can be used if the number of parameters is not clear

void foo(int a,...)
void foo(...).
Copy the code

5.2 the return value

There should also be a return statement after a loop that contains a return statement. If there is no return statement, it is an error. If the compiler does not detect this error, the runtime behavior is undefined.

for(int i=0; i<n; i++)if(i>8)
        return;
return;		// Must be written to prevent a final non-return
Copy the code

Do not return a reference or pointer to a local object. The space used by a local variable is freed at the end of the function, so do not return a pointer or reference to a local variable. But it is ok to return the value of a local variable, because you are using copied data.

const string &manip(a)
{
    string ret;
    if(! ret.empty())return ret;	// Error, return a reference to a local variable
    else
        return "abv";	// error, this is a local temporary quantity, at the end of the function will be empty space
}
Copy the code

Returns a class type or reference, pointer, or other function whose members can be accessed using the result of the function call

auto z=shorterString(s1,s2).size();
// A function that returns a reference type can be an lvalue
char &get_val(string &s,int x)
{returns[x]; }int  main(a)
{
    get_val(s,x)=1;	// Modify as lvalue directly
}
Copy the code

You can also initialize the return using a list

return {1.2.3}	// Usually used to return the container type
Copy the code

5.3 overloading

If several functions in the same scope have the same name but different parameter lists (number of parameters or parameter types), they are called overloaded.

Note: Main cannot be overloaded

int fun(int a,int b);
int fun(int a)
int fun(char c)
int fun(int c)	// This is not an overload
int fun(int)	// This is not an overload either
Copy the code

Note that function arguments do not accept the top-level const, so by default it doesn’t matter whether the top-level const is added or not.

int fun(int a);
int fun(const int a);		// No, top-level const

int fun(int *a);
int fun(int * const a);		// No, top-level const

// If the parameter is a pointer or reference of some type, function overloading can be done by differentiating whether the parameter refers to a constant object or a nonconstant object, and const is underlying
int fun(int &);
int fun(const int &);		// We can override the underlying const
int fun(int *);
int fun(const int *);		// We can override the underlying const
Copy the code

Note the need to avoid ambiguities in function overloading!

Functions cannot be overloaded in different scopes!

5.4 Special Features

Default arguments can be assigned to function parameters (in a declaration)

int fun(int a,int b,int c=1);
It is possible to assign default values to one or more parameters, but once a parameter is assigned, all subsequent parameters must have default arguments
int fun(int a,int b=1.int c);		// fun(1,2) is ambiguous
// Note that you cannot modify an existing default value
int fun(int ,int ,int c=2);			/ / error
// But you can add default arguments
int fun(int a,int b=2.int c=1);		/ / rightThe default argument is responsible for filling in the missing tail argumentint fun(string a="123".int a=1.int b=2); Fun (,2.3);// error, default add tail default parameters, want to modify which front can not be skipped!
    			// Only trailing arguments can be ignored
fun("1".2+1);	// If you want to modify b, this program will not be as expected, so eliminate ambiguities
Copy the code

Inline functions, which avoid the overhead of function calls, do not go into the address area of the function. Instead, the compiler “pastes” the function code directly into the corresponding location. Therefore, it is not suitable for large functions, but only for small, frequently called functions.

inline int f(int a,int b)
{returna>b? a:b; }cout<<f(1.2) < <endl;
/ / equivalent to the
cout<<1>2?1:2<<endl;
Copy the code

Assert preprocessor macro

assert(expr)
    // If the expression is false, output information and terminate the program
    // If true, do nothing
Copy the code

5.5 Function Matching

Specific operations are as follows

void f(a);
void f(int);
void f(int.int);
void f(double.double=3.14);

f(5.6);

(1) identify candidate functions1.The same name as the function being called2.Its declaration is visible at the call point/ / no
(2) determine the feasible function1.The number of parameters is equal to the number of arguments provided by the call (it is possible to consider default parameters)2.Each parameter has the same type as the corresponding parameter type, or can be converted to form parameter type// get rid of 1,3
// If this step does not find a viable function, the compiler will report no matching function error3Find the function whose parameter type best matches the argument type.1.An argument is converted from an array type or function type to the corresponding pointer type to add the top-level layer to the argumentconstOr remove the top layer from the argumentconst
			2.throughconstThe transformation implements matching (underlyingconst)3.Matching through type promotion4.A match by arithmetic type conversion or pointer conversion5.Matching through class type conversions (if the difference between overloaded functions is just one more layerconstThe function matches multiple parameters based on the actual parameter type (constant or nonconstant) of the function call1.The match of each argument to this function is no worse than the match required by other viable functions2.At least one argument matches better than those provided by other viable functions. If no unique function is found after checking, the compiler reports information about ambiguous calls./ / f (42,2.56)
 // Typical ambiguity error
Copy the code

5.6 Function Pointers

Function Pointers point to functions, not objects.

int max(int a,int b);
// Declare a pointer to the function. Replace the function name with the pointer
int (*p)(int ,int);
// Assigning a function to a pointer can omit the address character
p=max;
p=&max;		// It doesn't matter whether you add it or not
// Dereference a pointer when calling a function
cout<<p(1.2) < <endl;
cout<<(*p)(1.2) < <endl;
// Function Pointers can be assigned 0 or nullptr to indicate that they are not pointing to any location
// When using overloaded functions, the context must clearly define which function is being used, not one-to-many
Copy the code