C++ second bullet: C+ spare tire (default parameters)

I believe everyone is familiar with the concept of spare tire in life. Whether it is literally or the extension of the word "spare tire", in fact, it is not only in life, but also in our C++, "spare tire" also exists. , This is our topic today-the default parameters, so why is it said that it is the "spare tire" in C+, I believe everyone will be clear after reading this article.

The concept of default parameters

The default parameter refers to specifying a default value for the parameter of the function when declaring or defining the function. When calling this function, if the actual parameter is not specified, the default value is used, otherwise the specified actual parameter is used.
After reading the concept of default parameters, I believe that everyone has a certain understanding of why it is called the spare tire in C++. Let’s use this example to deepen everyone’s impression.
example:

#include <iostream>
using namespace std;

void Function(int a = 1)
{
    
    
	cout << a << endl;
}
int main()
{
    
    
	Function();
	Function(20);
	system("pause");
}

Operation result: In the operation result of the
Insert picture description here
above code, the first one is the result of calling the Function() function, here we did not give the specified parameters, so the output result is 0, and the second one is the result of calling Function(10) Function, because we gave the actual parameter, so the output is 10.

Classification of default parameters

All default parameters

As the name implies, when a function is declared or defined, a default value is assigned to all the parameters of the function.
example:

void Function(int a = 10, int b = 30, int c = 60)
{
    
    
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}

Semi-default parameters

Refer to the definition of full default parameters. Semi-default parameters mean that when a function is declared or defined, a default value is not specified for all the parameters of the function.
example:

void Function(int a , int b = 30, int c = 60)
{
    
    
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}

Notes on using default parameters

1. The semi-default parameters must be given in order from right to left, and cannot be given at intervals.
For example:

void Function(int a ,int b = 20,int c )
{
    
    }//错误

2. The default parameters cannot appear in the function declaration and definition at the same time.
Because if the declaration and definition positions appear at the same time, and the values ​​provided by the two positions are different, the compiler cannot determine which default value should be used.
For example:

//a,h 
void Function(int a = 10);

//a.c
void Function(int a = 50)
{
    
    

}

3. The default value must be a constant or a global variable

int d = 20;
void Function(int a = d ,int b = 20,int c )
{
    
    }//错误

4. C language does not support

Guess you like

Origin blog.csdn.net/qq_43825377/article/details/109171750