C + + learning path - default parameters

1, definition - definition

The default parameter value of the function, that is, when defining the parameter, give it an initial value at the same time. When calling a function, we can omit parameters with default values. That is, if the user specifies a parameter value, the user specified value is used; otherwise, the value of the default parameter is used. C + + allows functions to set default formal parameters. Actual parameters can be omitted when calling.

2, Default parameter usage example - Example

  • All formal parameters are default parameters
int sum(int v1 = 5, int v2 = 6)
{
	return v1 + v2;
}

int main(int argc, char **argv)
{
	cout << sum() << endl;//11. Default v1=5 v2=6
	getchar();
	return 0;
}
  • Some formal parameters are default parameters
int sum(int v1 , int v2 = 6)
{
	return v1 + v2;
}

int main(int argc, char **argv)
{
	cout << sum(5) << endl;//11. v1 incoming 5 default v2=6
	getchar();
	return 0;
}
  • The default parameter can also be a global variable
int global_value = 20;
int sum(int v1 = 5, int v2 = global_value, int v3 = 0)
{
	return v1 + v2 + v3;
}
int main(int argc, char **argv)
{
	cout << sum() << endl;//25, default V1 = 5 V2 = Global_ value=20 v3=0
	getchar();
	return 0;
}

3, Cases that do not constitute default parameters (error prone points) - careful

The default parameters can only be in right to left order (because the arguments are passed from left to right when the function is called)
Therefore, if you want to set default parameters for the formal parameters of a function, you must ensure that there are default parameters at the end of the formal parameter list

  • The following two are violations of the rule that there must be default parameters at the end of the formal parameter list
int sum_1(int v1 = 5, int v2)
{
	return v1 + v2;
}
int sum_2(int v0´╝îint v1 = 5, int v2)
{
	return v1 + v2;
}

int main(int argc, char **argv)
{
	cout << sum_1(10) << endl;//The result will not return 15, but an error, because C + + will think that 10 is passed to v1
	cout << sum_1(10,20) << endl;//The result will not return 35, but an error, because C + + will think that 10 is passed to v0 and 20 is passed to v1
	getchar();
	return 0;
}

4, Default parameters other common properties

If there are both declaration (. h file) and Implementation (. cpp) in the program, the setting of default parameters can only be placed in
In the function declaration, that is, when the. cpp file implements the function, the default parameters cannot be passed in the formal parameter list

.h file
int sum(int v1 = 5, int v2 = 10);

.cpp file-Error implementation
int sum(int v1 = 5, int v2 = 10)//Default parameters should not be passed in again here
{
	return v1 + v2;
}
.cpp file-Correct implementation
int sum(int v1, int v2 )
{
	return v1 + v2;
}

The default parameter can be either a constant, a global 'variable', or a function name

//The default parameter can be a constant
int sum(int v1 = 5, int v2 = 10)
{
	return v1 + v2;
}

//The default parameter can be a global variable
int global_value = 20;
int sum_(int v1 = 5, int v2 = global_value, int v3 = 0)
{
	return v1 + v2 + v3;
}

//The default parameter can be the function name (pointer to the function name)
void Pointer(int a)
{
	cout << "Pointer function -" <<a << endl;
}
//The default argument can be a pointer to a function
void function_point(int v1, void(*p)(int)=Pointer)
{
	p(v1);//Execute the function of Pointer function to print the value of v1
	//void(*p)(int)=Pointer explanation: int is the type of Pointer function parameter, and void(*p) is the type of function return value
}

int main(int argc, char **argv)
{
	cout <<function_point(5) << endl;//Print Pointer function - 5
	getchar();
	return 0;
}

The default parameter can be a pointer to a function. The Enlightenment of this function is that when a function wants to call another function internally, it can use the default parameter

Function overloading and default parameters may conflict and have ambiguous meanings. It is recommended to use default parameters first

void function1(int a, int b = 20)
{
	cout << "a is" << a << endl;
}
void function1(int a)
{
	cout << "a is" << a << endl;
}

int main(int argc, char **argv)
{
	cout <<function1(5) << endl;//An error will be reported. C + + doesn't know whether this 5 is passed to the first or the second
	getchar();
	return 0;
}

5, Nature of default parameters

Although there are default parameters, parameters are actually passed. The value of the default parameter is passed to the formal parameter as an argument. (it's all the work of the compiler)

  • Explain the nature of the code below
int sum_1(int v1 = 5, int v2 = 10)
{
	return v1 + v2;
}
int sum_2(int v1 , int v2 )
{
	return v1 + v2;
}
int main()
{
	sum_1();
	//Assembly code:
		mov edx 5
		mov edx 10
		call 0015205431032531
	sum_2(5,10);
		//Assembly code:
		mov edx 5
		mov edx 10
		call 0015205431032531
}

It can be seen that there is no essential difference between the assembly codes of the two functions, except that for functions with default parameters, the compiler directly passes the value to return v1 + v2; For functions without default parameters, first pass the value to the formal parameter to become an argument, and then the function passes the argument to return v1 + v2

6, Irrelevant learning tips

  • Pointer to function: function return value (* pointer name) (function parameter list, write type only) = function name
    For example: void(*pointer_)(int) = Pointer;
  • Then you can pass parameters to the pointer and call the function pointed to by the pointer
    For example: (* pointer_)(5);

Tags: C C++

Posted on Wed, 17 Nov 2021 05:00:41 -0500 by electricshoe