Related functions (C ++ language)

First, why should function

  • After you've written a function that can be reused, you can only care about the function and use of functions when used without concern for function-specific implementations.

  • It encourages code reuse and improve development efficiency and enhance reliability of the program, but also easy to maintain and modify the division of labor.

  • For example the following procedure: an 8-bit binary input is converted to a decimal output

#include <iostream>
using namespace std;

//计算x的n次方
double power(double x,int n)
{
    double val=1.0;
    while (n--)
        val *= x;
    return val;
}

int main()
{
    int value = 0;
    cout<<"Enter an 8 bit binary number: ";
    for(int i=7;i>=0;i--)
    {
        char ch;
        cin>>ch;
        if(ch=='1')
            value+=static_cast<int>(power(2,i));
    }

    cout<<"Decimal value is "<<value<<endl;
    return 0;
}


Second, why use the function overloading

  • Overloaded definitions:

    Two or more functions, with the same function name, but a different number or type parameter, the compiler according to the best match of arguments and types and number of parameters, which automatically determines a function call, the function which is It overloaded.

  • Overloaded benefits:

    Overloaded function makes C ++ programmers with different functions to perform a similar function can be unified name, reduce the mind naming spent. For example, you may need to seek a maximum of the function of two integers, it may have to write a maximum of three real function of demand, the function of these two functions are seeking the maximum, then both named Max , you do not need a named MaxOfTwoIntegers, another named MaxOfThreeFloats.

  • [Note] overloaded function parameter must be different: a different number or different types.

    E.g:

int add(int x,int y);
float add(float x,float y);
//二者形参类型不同

int add(int x,int y);
int add(int x,int y,int z);
//二者形参个数不同
  • Example overloaded function (written in two named sumOfSquare overloads, two squares are squared and two integers and real numbers)
#include<iostream>
using namespace std;

int sumOfSquare(int a,int b)
{
    return a*a+b*b;
}

double sumOfSquare(double a,double b)
{
    return a*a+b*b;
}

int main()
{
    int m,n;
    cout<<"Enter two integer:";
    cin>>m>>n;
    cout<<"Their sum of square: "<<sumOfSquare(m,n)<<endl;

    double x,y;
    cout<<"Enter teo real number:";
    cin>>x>>y;
    cout<<"Their sum of square: "<<sumOfSquare(x,y)<<endl;

    return 0;
}


Third, what is the value passed

  • Refers to the value passed when a function call occurs, the parameter to allocate memory space, and to initialize a parameter arguments (argument values ​​directly transmitted to the parameter). This process is a single pass process parameter values, once the parameter value will be gained from the relationship with the argument, then no matter what happens parameter changes will not affect the argument.
#include<iostream>
using namespace std;

void swap(int a,int b)
{
    int t=a;
    a=b;
    b=t;
}

int main()
{
    int x=5,y=10;
    cout<<"x= "<<x<<"  y="<<y<<endl;
    swap(x,y);
    cout<<"x= "<<x<<"  y="<<y<<endl;
    return 0;
}


Fourth, what is the address delivered

  • Incoming address of a variable, the function can be variable according to the address, access to the variable nature can change the value of a variable

  • As program
#include<iostream>
using namespace std;

double add(double,double);
double add2(double x,double y);

double calculate(double x1,double y1,double(**f)(double,double))
{
    cout<<"add:"<<(*f[0])(x1,y1)<<endl;
    cout<<"add2:"<<(*f[1])(x1,y1)<<endl;
    return 1;
}

int main()
{
    int x,y;
    double(*pf[2])(double,double)={add,add2};
    x = 2;y = 1;
    calculate(x,y,pf);
    system("pause()");
    return 0;
}

double add(double x,double y)
{
    return x+y;
}

double add2(double x,double y)
{
    return x-y;
}


Fifth, recursive functions

  • Function may call itself either directly or indirectly, to be called recursively

  • There are two stages of recursive

    The first stage: recursion.
    The second stage: the return.

* Actual:
> k calculated by the recursion selected individuals from the n individual committee consisting of a number of different combinations

#include<iostream>
using namespace std;

//计算从n个人里选k个人的组合数
int comm(int n,int k)
{
    if(k>n)
        return 0;
    else if(n==k||k==0)
        return 1;
    else
        return comm(n-1,k)+comm(n-1,k-1);
}

int main()
{
    int n,k;
    cout<<"Please enter two integers n and k: ";
    cin>>n>>k;
    cout<<"C(n,k)="<<comm(n,k)<<endl;
    return 0;
}

  • Tower of Hanoi problem
#include<iostream>
using namespace std;

//把src针的最上面一个盘子移动到dest针上
void move(char src,char dest)
{
    cout<<src<<"-->"<<dest<<endl;
}

//把n个盘子从src针移动到dest针,以medium针作为中介
void hanoi(int n,char src,char medium,char dest)
{
    if(n==1)
        move(src,dest);
    else
    {
        hanoi(n-1,src,dest,medium);
        move(src,dest);
        hanoi(n-1,medium,src,dest);
    }
}

int main()
{
    int m;
    cout<<"Enter the number of diskes:";
    cin>>m;
    cout<<"the steps to moving "<<m<<" diskes:"<<endl;
    hanoi(m,'A','B','C');
    return 0;
}

Guess you like

Origin www.cnblogs.com/xihuashi/p/11519714.html