2021-09-27 default parameters, placeholder parameters and function overloading of C + + functions

Function improvement

  1. Function default parameters

If no data is passed in the function, the default parameters in the function will be used:

int func(int a, int b=20, int c=30 )  //  20 is the default value of int b
{
	return a + b + c;
}

int main()
{
	cout << func(10) << endl;   // 10 is the value of a in func function
		system("pause");
    	return 0;
}


If we pass in data, we will use it. If not, we will use the default value:

int func(int a, int b=20, int c=30 )
{
	return a + b + c;
}

int main()
{
	cout << func(10) << endl;
		system("pause");
    	return 0;
}


matters needing attention:
1. If a location already has default parameters, it must have default values from this location, from left to back, and not before. That is, if b has a default parameter, c must also have one, otherwise an error is reported.
2 if the declaration of a function has default parameters, the implementation of the function cannot have default parameters.
Namely
int func( int a=10 ,int b=10); // statement
Int func (int a = 10, int b = 10) / / implementation
{
}
An error will be reported because the compiler does not know whether to use declared data or implemented data. There can only be one default parameter.

  1. Function placeholder parameter
    There can be placeholder parameters in the formal parameter list of the function, which must be filled when calling the function.
    Format:
    Return value type function name (data type)
    {
    }
int func(int a, int)  //Occupancy parameter
{
	return a ;
}

int main()
{
	cout << func(10,8) << endl;
		system("pause");
    	return 0;
}

Placeholder parameter combined with default parameter

int func(int a, int = 10)  //Placeholder parameter combined with default parameter
{
	return a ;
}
  1. Function reload
    In general, duplicate function names cannot coexist
    Function names can be the same to improve reusability.
    Need to meet:
    1 under the same scope.
    2. The function name is the same.
    3. Function parameters have different types, numbers, or orders.

For example:

 void func()          //   1
{
	cout << "call func()" << endl;
}
void func(int a)     //    2
{
	cout << "call func(a)" << endl;;
}
void func(double a)     //    3
{
	cout << "call func(double a)" << endl;;
}
void func(double a,int b)     //    4
{
	cout << "call func(double a,int b)" << endl;;
}
void func(int a, double b)     //    5
{
	cout << "call func(int a,double b)" << endl;;
}
int main()
{
	func();
	func(10);
	func(3.14);
	func(3.14, 10);
	func(10, 3.14);
		system("pause");
    	return 0;
}

If all func() functions are called at the same time, 1, 2, 3, 4 and 5 will be distinguished by the number of parameters. 2 and 3 are distinguished by the type of function. 4 and 5 are distinguished by the order of types. So even the same name will not affect each other. That is, the function is reloaded.

Note: the return value of a function cannot be used as a condition for function overloading. Because it is impossible to overload functions that can only be distinguished according to the return type [i.e. void func() and int func(), but only those on void and int].

  1. Considerations for function overloading

1 refers to the condition as a function overload

 void func(int &a)  //  1
{
	cout << "call func(int &a)" << endl;;
}
void func(const int& a)    //    2
{
	cout << "call func(const int& a)" << endl;;
}
int main()
{
	int a = 1;
	func(a);
	func(10);
		system("pause");
    	return 0;
}

Difference: because a = 1; A is a variable -- readable and writable, so func(a); Function 1 is called. func(10) has determined the number 10 - read-only and immutable, so it will call function 2 with const parameters. Basically, if func(10) calls function 1, it will become a statement like int & A = 10, which is illegal. On the contrary, in const int & A = 10, the compiler will optimize and modify the code and create a temporary data temp, Let a point to the temporary space. int temp =10 ; const int & a =temp; And cannot be modified.

2 function overload encounters default parameters

void func(int a,int b=20)   //      1
{
	cout << "call func(int a,int b=20)" << endl;;
}
void func(int a)       //     2
{
	cout << "call func(int a)" << endl;;
}
int main()
{
	func(10);
		system("pause");
    	return 0;
}

When using func(10); When, function 1 can be called, that is, the default parameters are used. Function 2 can also be called. So it will report an error.

If func(10,20); The above situation will not occur and function 1 will be called directly.

Tags: C++

Posted on Tue, 28 Sep 2021 04:43:48 -0400 by vito336