C ++ function summarizes


Why use function

  • Function may use a more complex system is divided into a plurality of blocks of program modules simple, make the program more clear

    比如,我们想要模拟一个栈,我们的入栈,出栈,判空等操作可以封装在push(),pop(),empty()中,可以更加清晰明了的了解到每一步的操作,易于理解程序。
  • Operating a defined functions can be repeated for a module, a simple function name, simple parameters to achieve this function call, the structured programming more convenient, easy to implement

//例如对于我们的程序需要n多次快速排序的操作,我们不需要写n次排序算法,而只是需要编写一次排序算法并将其封装为函数,通过调用函数进行n次排序
void QuickSort(int a[],int l,int r)     
{ 
    int i,j,p;
    p = a[l];
    i = l,j = r;
    while(i < j){
        while(i < j && a[j] >= p){  
            j--;
        } 
        if(i != j){
            a[i] = a[j];
        } 
        else break;
        while(i < j && a[i] <= p){  
            i++;
        }
        if(i != j){
            a[j] = a[i];
        }
        else break;
    }
    a[j] = p;   
    
    if(l < j) QuickSort(a,l,j);
    if(j+1 < r)QuickSort(a,j+1,r);
}

当定义好函数时,当我们再次需要执行数组由小到大的排序时,就可以直接调用QuickSort()与三个参数来完成
  • The packaging operation is a function, we can increase the efficiency Debug difficult to modify the code later can be reduced.

Why use function overloading

Function overloading can reduce the programmer from the function name, the name of difficult aspects of the mind functions. Easy call.

//我们以最简单的add()函数来举例,加入我们只定义一个int整形返回值的函数。那么该函数的作用就十分有限。
int add(const int &a,const int &b) (return a+b);
//但是当我们定义长整型,浮点类型时,函数的使用范围就会大大增加
long long add(const long long &a,const long long &b) (return a+b);
double add(const double &a,const double &b) (return a+b);
//这样我们就可以使用add函数来运算常见的几种基本类型

C ++ parameter passing mode

C ++, there are three ways of mass participation, value is passed, passing the address of the reference.

Value transfer

//值传递的最主要的特点就是:在函数中对形参的操作不会对其对应的实参产生影响。
void func(int x) {x -= 10;}

int t = 10;
func(t);
//该函数在函数中对x进行了操作,但形参x的改变并不会改变实参t。

Delivery address

//地址传递与值传递存在相同之处,即在函数体中的操作不会改变传入的指针的指向,但会改变指针指向的对象的值。
void set(int *p,const int &x) {*p = x;}

int t = 10;
int *p = &t;
int x = 0;
set(p,x);
//该函数运行后,p指针的指向不会发生改变,但是p指针指向的对象t的值被赋值为了x。

Passed by reference

//传引用参数与其他引用一样,可以想象为重新给了实参一个名字,其与实参共用同一地址。
void set(int &p,const int &x){p = x;}

int t = 10;
int x = 0;
set(t,x);
//当调用set()函数时,p绑定到对象t上,此时,改变p的值也就是改变t的值,因此,此时t的值为0。

//引用传递的另一个重要的用法是,返回额外信息,我们知道每个函数只能存在一个返回值,但是通过引用传递,可以在函数外定义一个实参来接收返回值,在函数内将其传递给对应形参。
//例如在模拟队列的pop()操作中,可以存储一个形参e接收被pop的值。
void pop(/*其他参数.. ,*/ int &e){
    e = que.front();//队列队头元素
    que.pop();  //队列的Pop操作
}

Special function - a recursive function

We know that a function can be called by other functions, and other functions may also include its own, this function is a recursive function.
Recursion can save our code length, but the attendant is not easy to understand and other disadvantages.

//比如经典的递归函数求Fibonacci数列的第n项
int fib(int n)
{
    if(n <= 2) return 1;
    return fib(n-1) + fib(n-2);
}
//优点固然可喜,但缺点也很严重,比如递归层数有限,又如对空间时间的浪费。
//所以,当我们想要使用递归函数时,要事先思考其递归层数防止栈溢出,思考它在时间空间上是否浪费过大等。

Guess you like

Origin www.cnblogs.com/rainyy/p/11516856.html