1. Why use function
First, look at the function: object-oriented programming, object-oriented design function is the function of the abstract
The role of the function:
( 1) easy to call, to improve development efficiency
( 2) reduce the complexity of the procedure, concise, easy to understand the program structure
Similarly prepared two integer add a program, it can be (see below)
1 //a+b不用函数 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 6 int a, b; 7 cin >> a >> b; 8 cout << a + b; 9 return 0; 10 }
Such may be (see below)
1 #include<iostream> 2 using namespace std; 3 int add1() 4 { 5 int a,b,c; 6 cin >> a >> b; 7 c =a + b; 8 cout << c; 9 return c; 10 } 11 int main() 12 { 13 add1(); 14 return 0; 15 }
But the longer the code for a program (FIG), without using the function of the fundamental unintelligible
2. Why use function overloading
(1) of the same name to avoid confusion function
(2) do not have to write a plurality of functions and parameters for different types or number of parameters, to improve the efficiency of programming
(3) make the program structure more clearly, you can use a variety of implementations to achieve unified interface, more in line with human habits
Such as
1 #include <iostream> 2 3 using namespace std; 4 5 void f(){ 6 cout<<"first"<<endl; 7 } 8 9 void f(int x){ 10 cout<<"second"<<endl; 11 } 12 13 void f(int x,int y){ 14 cout<<"third"<<endl; 15 } 16 17 void f(double x){ 18 cout<<"double"<<endl; 19 } 20 21 int main(){ 22 f(); 23 f(1); 24 f(1,2); 25 f(1.4); 26 return 0; 27 }
To call different functions depending on the parameter of the same name
3. What is the value of delivery
It refers to the value passed when calling the function argument passed to the copy function, so that if the function to modify the parameters, the argument will not affect
Popular terms is passed value only changes in the function of receiving, without affecting the function of the original value of the parameter
Such as
1 #include<iostream> 2 using namespace std; 3 4 void swap1(int a, int b) 5 { 6 int t; 7 t = a; 8 a = b; 9 b = t; 10 } 11 12 int main() 13 { 14 int a, b; 15 cin >> a >> b; 16 swap1(a, b); 17 cout << a << b; 18 return 0; 19 }
It can be expected, since the transfer characteristic values, the program outputs a, b values and does not change
4. What is the address delivered
Refers to the delivery address is the address of the arguments to the function, it is possible to modify the value of the argument in the function
We then modify the above code again
1 #include<iostream> 2 using namespace std; 3 4 void swap1(int &a, int& b)//修改行 5 { 6 int t; 7 t = a; 8 a = b; 9 b = t; 10 } 11 12 int main() 13 { 14 int a, b; 15 cin >> a >> b; 16 swap1(a, b); 17 cout << a << b; 18 return 0; 19 }
Now the program can output the results of exchange
The recursive function
An important feature of recursive function is directly or indirectly call the function itself, but for the average person's thinking is difficult to understand, let's remember that this feature is temporarily
In addition, the classroom teacher Tower of Hanoi recursive implementation, that is, start n the beginning, and then the implementation of n-1 operation, so we can not help but think of mathematical induction
So we may assume:
- When n = 0, 1 when the result is correct.
- N is assumed for correct function, a function of n-1 results are correct.
In this way we can avoid thinking of iterations to some extent.
With that deed Fibonacci number column, for example, we can write a recursive function:
1 int F ( int n) 3 2 { if (n == 1 || == 2 ) 4 { 5 return 1 ; } Else { 8 7 6 9 return gunman (n- 1 ) + gunman (n- 2 ); 10 } 11 }
if ...... else ...... statement is the use of 1,2 hypothetical conclusion