c++ parameter passing

Parameter passing

In the English term of parameter passing, the parameter is argument, that is, the actual parameter, and the parameter passing is to pass the actual parameter, or assign it to the formal parameter.

A formal parameter is essentially just a local variable inside a function. It is declared in the parameter list, which just means that when calling the function, we will assign the value of the first actual parameter to the first formal parameter. Just like there are three envelopes on the counter of a company, three employees put three documents into these three envelopes, after that, these three employees will send the documents to the intended destination, and someone will check the corresponding documents. Editing.
So to put it simply, the formal parameter is the operable container, the actual parameter is the content to be put in, and the parameter passing is to put the actual parameters into several containers in the specified order.


pass by value

There are three main ways to pass parameters in C++: pass by value, pass by pointer, and pass by reference.
Their essence is the same as the initialization of variables, pointers and references.

For primitive types, passing by value is simply passing the actual parameter to the formal parameter. For custom objects, copy assignment is also involved.

pass by value

#include<iostream>
#include<string>
using namespace std;

//按值传递

void printThings(int intNum,float floatNum,string str)
{
    
    
	cout << "打印整数:" << intNum << endl;
	cout << "打印浮点数:" << floatNum << endl;
	cout << "打印字符串:" << str << endl;
}
int main()
{
    
    
	int a = 3;
	float b = 1.0f;
	string str = "string";
	printThings(a,b,str);
	return 0;
}

The functions in the running result
insert image description here
example take three types of parameters to pass, which are simple assignments for integers and floating-point types. Since string is a custom object, it needs to call string overload when copying it to the formal parameter The assignment operation to copy.


pointer passing

Passing by pointer is essentially the same as passing by value, except that ordinary objects are replaced by pointers. Since the size of the custom object may be large (such as the string in the previous section), copying by assignment is time-consuming. In this case, using pointers is very efficient, because it only needs to upload data of the size of the address. Not only for parameters, but in any case we should try to use pointers to custom objects.

Another situation where passing by pointer is used is when we want to change the actual parameter in the function.

Pass by value to implement swap

#include <iostream>
using namespace std;

//按值传递实现swap

void swap(int a, int b)
{
    
    
	int temp = a;
	a = b;
	b = temp;
	cout <<"在交换函数末尾,a等于" << a << ",b等于" << b << endl;
}

int main()
{
    
    
	int a = 3;
	int b = 4;
	cout << "交换前,a等于" << a << ",b等于" << b <<endl;
	swap(a, b);
	cout << "交换后,a等于" << a << ",b等于" << b <<endl;
	return 0;
}

Running Results
insert image description here
It can be seen that at the end of the swap() function, formal parameters a and b are successfully exchanged. So why did a change back to 3 after the function was released? As mentioned before, the formal parameter is essentially a local variable whose scope is limited to the inside of the function. The actual parameter just assigns or copies the value to the formal parameter. After leaving the scope of the function, the formal parameter disappears, while the actual parameter remains the same.

Pointer passing implements swap

#include<iostream>
using namespace std;

//指针传递实现swap

void swap(int *pa,int *pb)
{
    
    
	int temp = *pa;
	*pa = *pb;
	*pb = temp;
	cout << "在交换函数末尾,a等于" << *pa <<",b等于" << *pb << endl;
}

int main()
{
    
    
	int a = 3;
	int b = 4;
	cout << "交换前,  a等于" << a <<",b等于" << b << endl;
	swap(&a, &b);
	cout << "交换后, a等于" << a <<",b等于" << b << endl;
	return 0;
}

Running results
insert image description here
This time after the swap ends, a and b also retain the exchanged results, because we pass the address, and modify the address of the formal parameter to be the same as the address of the actual parameter through the dereferencing operation in the function.
It should be noted that passing parameters by pointer is essentially passing by value, but the copy is not the entire file, but the address of the electronic version of the file.


pass by reference

Number swapping can also be achieved by passing parameters by reference:

Implementing swap by reference and passing parameters

#include<iostream>
using namespace std;

//引用传递实现swap
void swap(int &a,int &b)
{
    
    
	int temp = a;
	a = b;
	b = temp;
	cout << "在交换函数末尾,a等于" << a << ",b等于" << b << endl;
}

int main()
{
    
    
	int a = 3;
	int b = 4;
	cout << "交换前, a等于" << a <<",b等于" << b << endl;
	swap(a, b);
	cout << "交换后, a等于" << a <<",b等于" << b << endl;
	return 0;
}

Running results
insert image description here
As you can see in the example, except that the "&" symbol needs to be added when the formal parameter is declared, the rest of the place is the same as the example of passing by value, so that there is no need to consider the issues of dereference and value address. Therefore, using references to pass parameters in C++ is a very efficient, concise and error-prone method.

Guess you like

Origin blog.csdn.net/m0_62870588/article/details/124140091