C++ Review Road Six - Function

Function: The definition of a function usually includes: return value type, function name, formal parameter list and function body composition.

1. Function call

The calling of the function mainly completes two tasks: 1. Initialize the formal parameters corresponding to the function with the actual parameters. 2. Give control to the called function.

[Note]: The type of the actual parameter and the formal parameter must be the same .

The return type of the function

Most types can be used as the return type of a function, but arrays cannot be used as the return type of a function. There is also a special return type called the void type, which means that the function does not return any value.

3. Local objects

In C++, names have scope and objects have lifetimes.

The scope of a name is the part of the program text in which the name is visible.

The lifetime of an object is the period of time during which the object exists during program execution.

local variable

Formal parameters and variables defined inside the function body are called local variables. They are local to the function and are only visible within the scope of the function. Local variables also return to all other declarations with the same name hidden in the outer scope .

automatic object

For the object of a common local variable, the object will be created when the control path of the function passes through the definition statement of the variable, and the object will be destroyed when it reaches the end of the block where it is defined. Objects that only exist during the execution of the block are called automatic objects .

[Note]: A formal parameter is an automatic object .

local static object

When defining a local variable, define it as a static type, so it becomes a local static object. The declaration cycle of a local static object runs through the function call and beyond .

A local static object is initialized when the program's execution path passes through the object's definition statement for the first time, and it is not destroyed until the program terminates. During this period, the execution of the function where the object is located will not affect it.

[Note]: If the local static object has no explicit initialization, it will be initialized to 0 by default.

Let's look at an example:

#include <iostream>

using namespace std;

int fun();//fun function declaration

intmain()
{
	for (int i = 0; i < 3; ++i)
	{
		cout << fun() << " ";//Call the fun function in a loop
	}
	cout << endl;
	system("pause");
	return 0;
}

int fun()
{
	static int a = 0;//Define a local static object
	return ++a;
}

Finally, the program outputs 1 2 3 . As mentioned above, a local static object is only initialized when the function in which it resides passes through its definition statement for the first time .

Let's compare the example if a is not defined as a local static object.

#include <iostream>

using namespace std;

int fun();//fun function declaration

intmain()
{
	for (int i = 0; i < 3; ++i)
	{
		cout << fun() << " ";//Call the fun function in a loop
	}
	cout << endl;
	system("pause");
	return 0;
}

int fun()
{
	int a = 0;//Define a normal local object.
	return ++a;
}
The final result is 1 1 1. Every time the fun function is called, a will be initialized, because it is a common local object, so it will be destroyed after each function is executed.
Fourth, the function declaration

The name of the function must also be declared before the call. A function declaration is similar to a function definition, but a function declaration does not require a function body, and also does not need to write the name of the formal parameter, just add a semicolon after the function name.

E.g:

int fun(); //This is a function declaration.
Five, parameter transmission

pass-by-value parameters

1. Ordinary objects

All operations of the function on the formal parameters will not image the actual parameters, because such parameter passing copies the value of the actual parameter to the formal parameter, and the actual parameter and the formal parameter are two independent objects .

2. Pointer parameter

When we use a pointer as a parameter, the value of the pointer is copied at this time . After copying, the two pointers are different, but they can both indirectly access the objects they refer to, and the objects they refer to are the same , so we can use the pointer to modify the value of the object it points to .

pass-by-reference parameter

We can use reference parameters to change the value of the actual parameter, because the operation on the reference is actually the operation on the referenced object , and the reference just gives the object another name.

Let's take a look at how these three parameter passing differ

1. Ordinary object transfer

#include <iostream>

using namespace std;

void swap(int x, int y); //Declaration of ordinary parameter transfer function

intmain()
{
	int a = 3, b = 5;
	cout << "交换之前的值:" << " a = " << a << " b = " << b << endl;
	swap(a, b); //function call
	cout << "交换之后的值:" << " a = " << a << " b = " << b << endl;
	system("pause");
	return 0;
}

void swap(int x, int y) //function definition
{
	int t = y; //Define an intermediate variable to exchange two objects.
	y = x;
	x = t;
}

The final output is: 

Values ​​before swapping: a = 3 b = 5

Value after swap: a = 3 b = 5

2. Pointer parameter

#include <iostream>

using namespace std;

void swap(int *x, int *y); //Declaration of pointer parameter transfer function

intmain()
{
	int a = 3, b = 5;
	cout << "交换之前的值:" << " a = " << a << " b = " << b << endl;
	swap(&a, &b); //The actual parameter needs to take the address
	cout << "交换之后的值:" << " a = " << a << " b = " << b << endl;
	system("pause");
	return 0;
}

void swap(int *x, int *y) //function definition
{
	int t = *x; ​​//Define an intermediate variable to exchange two objects.
	*x = *y;
	*y = t;
}

The final output is: 

Values ​​before swapping: a = 3 b = 5

Value after swap: a = 5 b = 3

3. Passing reference parameters

#include <iostream>

using namespace std;

void swap(int &x, int &y); //Declaration of pass-by-reference parameter transfer function

intmain()
{
	int a = 3, b = 5;
	cout << "交换之前的值:" << " a = " << a << " b = " << b << endl;
	swap(a, b);
	cout << "交换之后的值:" << " a = " << a << " b = " << b << endl;
	system("pause");
	return 0;
}

void swap(int &x, int &y) //function definition
{
	int t = x; //Define an intermediate variable to exchange two objects.
	x = y;
	y = t;
}

The final output is: 

Values ​​before swapping: a = 3 b = 5

Value after swap: a = 5 b = 3

Through the above example, we can see that the call of the ordinary object will not change the value of the actual parameter, but we can change the value of the actual parameter through the pointer or reference.

[Note]: The call of ordinary parameters and pointers will copy the actual parameters to the formal parameters . The ordinary parameters copy the value of the parameter, while the pointer parameter copies the pointer. These two pointers are different. A reference does not need to be copied , because a reference is a direct operation on the actual parameter.

Recommendation : Because references do not need to be copied, it is best to use references when passing large structures.

6. Function overloading

Several functions in the same scope have the same name but different formal parameter lists . Such functions are overloaded functions.

E.g:

void fun(int a, int b);
void fun(char a);
void fun(int *a, const int *b);

The above functions are overloaded functions.

[Note]: The main function cannot be overloaded.

Seven, the default parameters

When calling a function with a default argument, the argument can be included or omitted.

E.g:

void fun(int a, int b = 5);//b is the default argument
// can be called like this in the face function
intmain()
{
  int x = 6, y = 9;
  fun(x);
}

If we print the values ​​of a and b in the fun function, it will output, 6 and 5; we call fun without passing the value of y, so the default arguments are used. But if we write fun(x, y) when we call it, the results printed in the fun function are 6 and 9.

[Note]: Once a parameter is assigned a default value, all the parameters following it must have a default value .

8. Inline functions

Just add inline before the return type of the function to become an inline function.

example:

inline int sum(int a, int b)
{
    int sum = a + b;
    return sum;
}

The above is an inline function.

What an inline function does: expand it "inline" at each call site. It is equivalent to expanding the content of the function body at the place where the function is called and putting it in the place where the function is called.

[Note]: Inline functions are suitable for small-scale, direct and frequently called functions .


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325949067&siteId=291194637