C + + foundation-3-function

Time:2022-5-14

3. Function

3.1 function default parameters

1 #include
 2 using namespace std;
 3 
 4 // default parameters of function
 5 // when you pass parameters, use your own. If not, use the default value
 6 int func(int a, int b = 20, int c = 30)
 7 {
 8     return a + b + c;
 9 }
10 
11 // Note:
12 //1. If there is a default parameter for a certain position, there must be a default parameter from left to right from this position
13 //2. If there are default parameters in the function declaration, the function implementation cannot have default parameters
14 // that is, only one declaration and implementation can have default parameters
15 int func2(int a = 10, int b = 20);
16 
17 //int func2(int a = 20, int b = 30) {
18 //    return a + b;
19 //}
20 // an error is reported. Redefine the default parameters
21 
22 int main() {
23 
24     cout << func(10, 20, 30) << endl;
25     cout << func2(10, 20) << endl;
26 
27     system("pause");
28 
29     return 0;
30 }
31 
32 // summary
33 // in C + +, the formal parameters in the formal parameter list of a function can have default values
34 // syntax: return value type function name (parameter = parameter default value) {}
35 //

3.2 function occupancy parameters

1 #include
 2 using namespace std;
 3 
 4 // function placeholder parameters. Placeholder parameters can also have default parameters
 5 void func(int a, int = 10) {
 6     cout << "a = " << a << endl;
 7 }
 8 
 9 int main() {
10 
11     int a = 10;
12     int b = 20;
13 
14     func(a, b);
15 
16     system("pause");
17 
18     return 0;
19 }
20 
21 // summary
22 // the formal parameter list can have placeholder parameters, which are used as placeholders. This position must be filled when calling the function
23 // syntax: return value type function name (data type) {}

3.3.1 function overloading overview

#include
using namespace std;

//Function overloading
//Make the function names the same to improve reusability

void func() {
    Cout < < function call of func < < endl;
}

void func(int a) {
    Cout < < "func function call!"<< endl;
}


int main() {

    func(3);

    system("pause");

    return 0;
}


//Summary
//Function: function names can be the same to improve reusability
//Function overloading满足条件:
//1. Under the same scope
//2. Same function name
//3. Function parameters have different types, numbers or orders
//Note: the return value of a function cannot be used as a condition for function overloading

3.3.2 precautions for function overloading

#include
using namespace std;

//Function overload considerations
//1. Reference as overload condition
Void func (int & A) {// int & A = 10; illegal
    Cout < < call of func (int &a) < < endl;
}

Void func (const int & A) {// const int & A = 10; // can convert
    Cout < < call of func (const int &a) < < endl;
}

//2. Function overload encountered default argument
void func2(int a, int b = 10) {
    Cout < < call of func2 (int a, int b) < < endl;
}

void func2(int a) {
    Cout < < call of func2 (int a) < < endl;
}

int main() {

    int a = 10;
    func(a); //  Call of func (int & A)

    func(10); //  Call of func (const int & A)

    //func2(10); // When the function overload encounters the default parameter, ambiguity and error are reported. Try to avoid it during programming

    system("pause");

    return 0;
}

//Summary
//Reference as overload condition
//Function overload encountered default parameter

Reference: dark horse programmer C + + tutorial