On the C ++ function chapter knowledge

On the C ++ function chapter knowledge

First, why should function

1, reusability: the function is defined, reusable function modules can be easily called in the program multiple times.
2, so that the program concise: function may be a more complex system is divided into a plurality of blocks of program modules simple, reduce the overall complexity of the process, easy to maintain and modify the division of labor.
For example, find the square of x plus y squared values:

#include<iostream>
using namespace std;
double fun(double x)//定义一个求平方的函数
{
    return x * x;
}
int main()
{
    double x, y,re;
    cin >> x >> y;
    re = fun(x) + fun(y);//分别调用fun()函数,再实现求和
    cout << re << endl;
    return 0;
}

Second, why use the function overloading

1, C ++ function allows functions similar names with the same functions in the same scope, thereby forming overloaded. Easy to use, easy to remember.

When the parameter type changed for a long time, if you do not use the overloaded function, the function name will change much, when you call to find the appropriate function name, is very complicated, for example the following:

//当参数类型变化时,需要重新定义不同名字的函数,使得声明函数和调用函数变得麻烦
int add1(int x,int y);
float add2(foat x,float y);
double add3(doublex,double y);

The use of overloaded functions to achieve the above functions:

//形参类型不同,定义相同名字函数,实现重载函数功能,调用时系统会自动调用相应参数类型的函数
int add(int x,int y);
float add(float x,float y);
double add(double x,double y);

Third, what is the value passed

It refers to the value passed when calling a function copies the actual parameter passed to a function, so that if the function parameter modifications will not affect the actual parameters
of illustration (super classic example, ha):

#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;
}

Output Results: The
Output
results of the analysis:
the swap function because it is passed by value, in fact, not the main function of the exchange of two values, because the argument passed to the parameter, the parameter re-opened a storage space, and storing the same arguments value , the swap function is actually operating parameter value, will not affect the argument.

Fourth, what is the address delivered

Address transmitting means when a call process is to pass the memory address of a variable argument to the called procedure parameter, i.e. shape memory cells involved in the argument with the same address. Therefore, when changing the value of the parameter in the called procedure, it changes the value equal to the argument.
Example: (The above swap function need only change a little bit switching functions can be achieved it!)

void swap(int *a, int *b)//通过地址传递
{
    int t = *a;
    *a = *b;
    *b = t;
}

Output Results:
result
Results Analysis:
by transmitting address, shape memory cells involved in the argument with the same address . Operation parameter is actually the operation of argument, so when changing the value of the parameter in the called procedure, changes the value is equal to argument

Address transfer characteristics:
1 to form the memory cell involved in the argument with the same address. Therefore, when changing the value of the parameter in the called procedure, it changes the value equal to the argument.
2, the memory can save extra overhead thunk binding, since only one argument passed to the address on the list, a view of an example:

#include<iostream>
using namespace std;
void sort(int *a)//通过地址传递
{
    int t;
    for(int i=0;i<9;i++)
        for (int j = 0; j < 10; j++)
        {
            if (*(a + j) < *(a + j + 1))
            {
                t = *(a + j);
                *(a + j) = *(a + j + 1);
                *(a + j + 1) = t;
            }
        }
}

int main()
{
    int a[10] = { 3,0,4,5,6,9,14,1,12,7 };
    sort(a);
    for (int i = 0; i < 10; i++)
        cout << a[i] << " ";
    return 0;
}

By passing the address, only the first address of the array is passed to the parameter, you can sort, which saves memory spending.

How Five, design experiments and lesson plans, writing recursive function analysis

Definition: The function calls itself directly or indirectly, called recursive call.
For example, seek 4! The value is the number? :

The preparation of two key recursive function:
1, break the problem into smaller parts, so that it can call itself a function
can break down like this:
Here Insert Picture Description

2, to ensure that the decomposition of the final issue, the problem is a known solution, that set export function.
Here Insert Picture Description
Example 1:
! Find the value of n.

unsigned fac(unsigned n)
{
unsigned f;
if(n==0)//关键!设置出口
f=1;
else 
f=fac(n-1)*n;//调用自身
return f;
}

Examples of the dicarboxylic ((difficult)):

Tower of Hanoi:
Analysis: three needles A, B, C, will be moved from the n disks A needle needle C can be divided into the following three steps

1, A is the upper plate moves to the n-1 B needle (needle means C): this is a recursive procedure, without considering the details
2, the rest of the needle a on a needle plate to move the C: can be done directly, recursive outlet
3, the needle B of the n- C 1 moved to the needle plate (by means of pin a): this is a recursive procedure, without considering the details

1,3 hanoi function implemented by the process, move the process function to achieve 2

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);
    }
}

Guess you like

Origin www.cnblogs.com/-believe-me/p/11519054.html