C++ function with parameter default value

#include<iostream>

int sum(int a, int b)
{
    
    
	return a + b;
}
int main()
{
    
    
	int a = 10;
	int b = 20;
	int ret = sum(a, b);
	std::cout << "ret:" << ret << std::endl;
	return 0;
}

The definition of sum function can have the following kinds:

int sum(int a=10,int b=20);
sum();
int sum(int a,int b=20);
sum(a);

But int sum(int a,int b=20) ; cannot be compiled, the reason is that the function push is pushed into the stack from right to left, and the default value is given from right to left.

Function call

int sum(int a=10, int b=10)
{
    
    
	return a + b;
}
int main()
{
    
    
	int a = 10;
	int b = 20;
	int ret = sum(a,b);
	
	//mov eax,dword ptr[ebp-8]
	//push eax
	//mov ecx,dword ptr[ebp-4]
	//push ecx
	//call sum
	
	std::cout << "ret:" << ret << std::endl;
	
	//push 14H
	//mov ecx,dword ptr[ebp-4]
	//push ecx
	//call sum
	
	ret = sum(a);
	
	//push 14H
	//push 0Ah
	//call sum
	return 0;
}

Is the efficiency of calling functions with default values ​​for formal parameters and ordinary functions improved? The
answer isHave, But the efficiency in individual cases will be the same as before, but the efficiency will not decrease.

int ret = sum(a,b);

//mov eax,dword ptr[ebp-8]
//push eax
//mov ecx,dword ptr[ebp-4]
//push ecx
//call sum

When the default value is given

int ret = sum(a,40);
//push 28H
//mov ecx,dword ptr[ebp-4]
//push ecx
//call sum

It can be clearly observed that the second piece of code executes one mov instruction less than the first piece of code, and the efficiency is naturally improved.

Where can the function default value be given?

1. Give the default value of the formal parameter during declaration.
Insert picture description here
2. Give the default value of the
Insert picture description here
formal parameter during definition. When the parameter is given the default value, can it be repeated? In other words, what happens if you give the same default value multiple times?

The same default value is
Insert picture description here
different.
Insert picture description here
Therefore, when a parameter is given a default value, whether it is defined or given, the default value of the parameter can only appear once, even if the default value is the same.
When the compiler executes the code, it is executed sequentially from top to bottom, and the default value is given from right to left, so the following form of initializing the default value is also possible.
Insert picture description here

Summary:
Function
1 with default values ​​for formal parameters is given from right to left when giving default values;
2. The efficiency of calling functions with default values ​​for formal parameters and ordinary functions is improved, but the efficiency in individual cases will be the same as before, but Efficiency will not decrease;
3. Both the definition and declaration places can give the default value of the formal parameter;
4. When the formal parameter is given the default value, whether it is defined or given, the default value of the formal parameter can only appear once, even if The default value is the same.

Guess you like

Origin blog.csdn.net/Gunanhuai/article/details/108629781