Three ways of passing function parameters in C++: passing by value, passing by address, passing by reference and the application of const in function parameters

1 pass by value

        When calling a sub-function, pass the value of the actual parameter in the main function to the formal parameter of the sub-function, and complete the corresponding operation in the sub-function. At this time, all operations in the sub-function are performed on the formal parameters, so the actual parameters in the main function are not affected by the sub-function.

And the formal parameters of the sub-function are stored in the stack area, and are automatically released by the compiler after the function call is completed!

Take swapping two numbers as an example:

(1) Subfunction

// 值传递
void swapab_value_transmit(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
}

(2) Main function

void main()
{
	int a = 10;
	int b = 20;

	// 值传递( 不改变实参的值 )
	swapab_value_transmit(a, b);
	cout << "值传递:" << "a=" << a << "	b=" << b << endl;
}

Effect:

Obviously, due to the value passing, the sub function does not complete the exchange of parameter values ​​in the main function!

2 address delivery

         When calling a sub-function, pass the address of the actual parameter in the main function to the formal parameter of the sub-function (at this time, the formal parameter of the sub-function uses a pointer to receive the address of the actual parameter). Therefore, at this time, the sub-function directly operates on the memory space pointed to by the address of the actual parameter , so the value of the actual parameter can be changed!

        Even if the formal parameter is released after the sub-function call is completed, since the formal parameter pointer points to the address of the actual parameter of the main function, the same operation is also performed on the actual parameter when the formal parameter is operated.

Take swapping two numbers as an example:

(1) Subfunction

// 地址传递
void swapab_addr_transmit(int *a, int *b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}

 (2) Main function

void main()
{
	int a = 10;
	int b = 20;

	// 地址传递( 直接对地址进行操作,可改变实参的值 )
	swapab_addr_transmit(&a, &b);
	cout << "地址传递:" << "a=" << a << "	b=" << b << endl;
}

Effect:

 Obviously, because the address is passed, the sub-function completes the exchange of the actual parameter values ​​in the main function!

3 pass by reference

  • The role of reference : Aliasing variables
  • Reference syntax : data type & alias = original name ( the data type must be consistent with the original variable )
  • Notes for reference : It must be initialized and cannot be changed after initialization (can no longer be aliased to other variables)
  • Advantages of using references as function parameters : you can achieve the same effect as passing an address and simplify your code and syntax !

        When the reference is used as a function parameter, the memory pointed to by the actual parameter name (original name) in the main function is taken as an alias (formal parameter of the sub-function) when the sub-function is called, that is, both the alias and the original name point to the same memory space . Therefore, the operation of the formal parameters in the sub-function is to directly operate the memory of the actual parameter, so as to achieve the effect of operating the actual parameter in the sub-function! And this writing method simplifies code writing!

Take swapping two numbers as an example:

(1) Subfunction

// 引用传递
// 引用给变量起别名,通过原名和别名都可访问变量的地址
void swapab_cite_transmit(int& a, int& b)
{
	int temp = a;
	a = b;
	b = temp;
}

(2) Main function

void main()
{
	int a = 10;
	int b = 20;

	// 效果与地址传递相同(直接对实参的地址进行操作),可改变实参的值
	swapab_cite_transmit(a, b);
	cout << "引用传递:" << "a=" << a << "	b=" << b << endl;
}

Effect:

 Obviously, because it is passed by reference, the sub-function completes the exchange of the actual parameter values ​​in the main function!

4 The application of const in function parameters

        The const keyword can define constants. Constants modified by const cannot be modified in the program. The basic syntax for defining constants with const is:

const type variable = value;

        The main function of using const in the function parameter list is: const-modified variables can change the corresponding variable to a read-only state to avoid misoperation on the original data !

4.1 Application of pass-by-value parameters

Function : Prevent misoperation of incoming variables!

(1) Subfunction

void test(const int a)
{
	// a = a + 1;     //会报错,const修饰的变量不能被修改
	cout << "a=" << a << endl;
}

(2) Main function

void main()
{
    int a = 10; 
    test(a);
}

4.2 Application of Address Transfer Formal Parameters

The main ways to modify pointer variables with const are:

  • Constant pointer : const type * p (the point of pointer p can be modified, and the value pointed to by pointer p cannot be modified )
  • Pointer constant : type * const p (the point of pointer p cannot be modified , and the value pointed to by pointer p can be modified)
  • That is to modify the pointer and modify the constant: const type * const p (the value pointed to by the pointer p and the value pointed to cannot be modified )

Function example:

void test1(const int * a)    // 常量指针,指向的值不能修改
{
	// *a = 2;     //会报错,指向的值不能修改
    int b = 20;    
    a = &b;       //正确,指针的指向可以修改
	cout << "a=" << *a << endl;
}
void test2( int* const a)	// 指针常量,指针指向不能修改
{
	int b = 20;
	//a = &b;		// 错误,指针指向不能修改
	*a = 20;        // 正确,指针的指向的值可以修改
	cout << "子函数 a= " << *a << endl;
}
void test2( const  int* const a)	// 指针常量,指针指向不能修改
{
	int b = 20;
	// a = &b;		// 错误,指针指向不能修改
	//*a = 20;      // 错误,指针的指向的值不能修改
	cout << "子函数 a= " << *a << endl;
}

4.3 Application of pass-by-reference parameters

Constant reference syntax (allows direct assignment of values):

// 1.可直接赋予数值
const int & name = 10;
// 编译器自动优化为: int temp= 10;  const  const int & name = temp;


// 2.也可赋予变量名
int a = 20;
const int & name = a;

Function : Mainly used to modify formal parameters to prevent misoperation !

void test(const int & a)
{
	//a = 20;            // 错误, const修饰的引用不可修改
	cout << "子函数 a= " << a << endl;
}

Therefore, there are two ways to call a constant reference as a function parameter:

void main()
{
    int a = 10; 
    test(a);         // 给变量名
	test(20);        // 直接给数值
}

 Effect:

 

Guess you like

Origin blog.csdn.net/jac_chao/article/details/124232275