C ++ function and its application

C ++ function and its application

One. Why use function

  1. We know, c and c ++ function is used, the amount of code can be simplified, to the various parts of the package, so that the problem becomes simple and intuitive, improving the readability of the program.

  2. May also enhance the maintainability, some calculations or operations knitted general function, for call at any time, thus avoiding tedious repetition code.

  3. But the use of function, you need to pass parameters, open the cache, stack, etc., comparatively speaking, it will consume some extra efficiency.

Example: Comparison of the size of the number three

#include <iostream> 
using namespace std;  
void func(int a,int b,int c)
{  
int middle,max,small;
if(a>b){
    max=a;
    middle =b;
       }
else{
    max=b;
    middle =a;
    }

if(c>max){
    cout << c << " " << max << " "<< middle;
    }
else if(c<middle){
cout << max << " " << middle << " " <<c;
    }
else{
cout << max << " " << c << " " <<middle;
    }
}

int main(){
int a,b,c;
cout << "请输入a,b,c的大小\n";
cin >> a >> b >> c;

func(a,b,c);//进行排序 

return 0;
}

  We can see that compared with the size of a number of three func function, the main function becomes simple and clear, readability becomes strong.

two. Function overloading

  In C ++, if you need to define several functions similar to, and different types of function parameters, then such several functions can use the same function name, which is "function overloading."
Two overloaded functions must differ in one or both of the following, only the different types of return value is not acceptable.
1, a different number of arguments;

2, the function of different parameters or a different type of parameter type sequence.

For example, summation function, corresponding to different types of parameters, number of overloaded functions may be defined as follows:

#include<iostream>
using namespace std;

int sum(int a=0,int b=0){
return a+b;
}
double sum(double a=0,double b=0){
return a+b;
}
float sum(float a=0,float b=0,float c=0){
return a+b+c;
}

int main(){

int x=sum(5,9);
float y=sum(2.7,5.87);
float z=sum(float(x),y,5);

cout << x << " " << y << " " << z; 
return 0; 
}  

three. What is the value of delivery

  When called, the value of the argument corresponding to the transmission parameter, that is passed by value. Function is worth any parameter modifications will not change the value of argument variables.
The classic example exchange x, y values:

#include<iostream>
using namespace std;

void swap(int x,int y){

int temp;

temp=x;
x=y;
y=temp;

cout << "swap中a和b " << x << " " << y << endl; 

}

int main(){

int a,b;

cin >> a >> b;

swap(a,b);

cout << "main中a和b " << a << " " << b << endl; 

return 0;
}

  We analyze the entire program: the main function, the arguments a and b have their own storage space, and has its own initial value. When Swap calling function, a memory function of x and y parameters allocated storage space, and copying the value of a and b over function is being executed, the values ​​of x and y are exchanged after the end of the function execution, and x y occupied storage space is released, this way of transmission, the argument value will not have an impact on a and b, the values ​​of a and b seen yo not change.

four. What is pass by reference

  Another alias reference variable is a variable, it does not have its own memory locations for storing data, it accesses the memory location of another variable. Any changes made to the reference variable, actually change the memory location of the variable it references the data is stored.
We pass program values, the void swap (int a, int b ) {...} to void swap (int & a, int & b) {...}. As can be seen, in the main function, the value of ab is changed.

Fives. How to write a recursive function

  Speaking of recursion, had said he wanted a child heard the story: There was once a mountain, the mountain there was a temple, the temple there was a monk, the monk in the story, once a mountain, the mountain there was a temple, the temple has monk, the monk in the story, once a mountain ...
spread of the internet this expression package, he is also very good interpretation of what is called recursion. Principle recursive stack mechanism is through the process of recursive function, and its sign is pushed and popped, and return values of these symbols and functions in the process calculates the stack.

  Let us look at the problem well understood Hanluo Ta: This problem is copper on a device, there are three rods (numbered A, B, C), A bottom-up lever, are placed in descending order of n a gold plate. Goal of the game: A bar of gold plate to move all rods C, and still maintain the original order folded.

  We will examine this issue recursive idea, put n disks from A-> C, we have to put a plate n-1 from A-> C-> B, then A and finally a dish from A-> C, followed by n-1 and then on a plate B-> A-> C from.
More specific point is before we take n-2 is moved onto a plate B, n-3 C is moved onto a plate, and so on, finally launched a first plate should be placed on the B or C. Since the reverse output is recursive, where we have been recording the penultimate dish should be placed, then added together the first two dishes, where to find a plate on the bottom third, and so on in the record in front, up to the n-1 plates stacked them complete the first step (n-1 we will put a plate from the A-> C-> B), the first move (n-1, x, z, y) recursive function calls complete end.
At this intermediate statements directly outputs the n disks from A-> C, to complete the second portion.
After completion of a move to the third function, the role of move (n-1, y, x, z), the function and the role of this function is similar to the first except for changing the starting needle, the needle and the intermediate needle purposes. The same principle and a recursive function. Since we know that the first and second recursive function is a recursive function similar step is the same, the number of steps can be derived must be odd Tower of Hanoi (plus the intermediate step of moving directly to a final dish).
Specific code:

//栈之递归汉诺塔问题
#include<iostream>

using namespace std; 

void move(int n,char x,char y,char z){

if(n==1)
cout << x <<"->" << z << endl;  //把x放到z上

else{
    
    move(n-1,x,z,y);
    cout << x <<"->" << z << endl;  
    move(n-1,y,x,z);
    
 }  
}

int main(){

move(3,'x','y','z');

return 0;

}  

Guess you like

Origin www.cnblogs.com/xqy-888/p/11520765.html