3- recursive call

1, call the function : when you call another function during the operation of a function, before running the called function, the system needs three things:

  • Passes all actual parameters, return address and other information to the called function.
  • For the local variables of the called function (including parameter) allocated storage space
  • Transfers control to the called function entry
#### 自己调用自己
# include <stdio.h>

void f(int n)
{
    if (n == 1)
        printf("込込\n");
    else
        f(n-1);
}

int main(void)
{
    f(3);

    return 0;
}

2, before being returned from the calling function called function, the system must accomplish three things:

  • Save the called function returns
  • Free up storage space is occupied by the called function
  • In accordance with the called function is stored in the return address transfer control to the calling function
#### 不同函数之间的相互调用
# include <stdio.h>

void f();
void g();
void k();

void f()
{
    printf("FFFF\n");
    g();
    printf("1111\n");
}

void g()
{
    printf("GGGG\n");
    k();
    printf("2222\n");
}

void k()
{
    printf("KKKK\n");
}

int main(void)
{
    f();

    return 0;
}

3, when there are a plurality of function calls to each other, according to "return to the first call" principle, information transfer and transfer control means between said function must "stack" is achieved, i.e. the system will be needed to run the entire program data space arranged in a stack, each time a function is called, will allocate a storage area in the top of the stack, perform a push operation, whenever a function exits, it releases its storage area, it was pop operations, currently running function always in the stack position.

4, A functions and function calls A B A function calls a function in the computer appears to be no different, but with our daily way of thinking more weird it.

2-1: Recursive three conditions must be met

a, recursive have to have a definite termination condition

b, the size of the data to be processed in the function decrements

c, the conversion must be solvable

3-1: advantages and disadvantages and recursive loop

Recursive:
1, easy to understand

2, slow

3, large storage space

Cycle
1, difficult to understand
2, speed
3, a small memory space

4-1 factorial recycling

# include <stdio.h>

int main(void)
{
    int val;
    int i, mult=1;

    printf("请输入一个数字: ");
    printf("val = ");
    scanf("%d", &val);

    for (i=1; i<=val; ++i)
        mult = mult * i;
    
    printf("%d的阶乘是:%d\n", val, mult);


    return 0;
}

Recursive summation

# include <stdio.h>

long sum(int n)
{
    if (1 == n)
        return 1;
    else
        return n + sum(n-1);
}

int main(void)
{
    printf("%ld\n", sum(100));

    return 0;
}

4-2 Tower of Hanoi

# include <stdio.h>

void hannuota(int n, char A, char B, char C)
{
/*
    如果是1个盘子
        直接将A柱子上的盘子从A移到C
    否则
        先将A柱子上的n-1个盘子借助C移到B
        直接将A柱子上的盘子从A移到C
        最后将B柱子上的n-1个盘子借助A移到C
*/
    if (1 == n)
    {
        printf("将编号为%d的盘子直接从%c柱子移到%c柱子\n", n, A, C);
    }
    else
    {
        hannuota(n-1, A, C, B);
        printf("将编号为%d的盘子直接从%c柱子移到%c柱子\n", n, A, C);
        hannuota(n-1, B, A, C);
    }
}

int main(void)
{
    char ch1 = 'A';
    char ch2 = 'B';
    char ch3 = 'C';
    int n;

    printf("请输入要移动盘子的个数: ");
    scanf("%d", &n);

    hannuota(n, 'A', 'B', 'C');


    return 0;
}

4-3 indirect call their own

# include <stdio.h>

void f(int n)
{
    g(n);
}

void g(int m)
{
    f(m);
}

int main(void)
{

    return 0;
}

Function calls functions B 4-4 A Example

# include <stdio.h>

int f(int n)
{
    int i, j;
    n += 2;  // n = n + 2;
    
    return n;
}

int main(void)
{
    int val;

    val = f(5);
    printf("val = %d\n", val);

    return 0;
}

Function calls functions B 4-4 A Example 2

# include <stdio.h>

int g(int);

int f(int n)
{
    if (n < 3)
        printf("込込\n");
    else
        n = f(n-1);

    return n;
}

int g(int m)
{
    m = m*2;
    return m;
}

int main(void)
{
    int val;

    val = f(5);

    return 0;
}

4-6 recursion is incremented value in reducing the size of the processing

# include <stdio.h>

int g(int);

int f(int n)
{
    if (n > 7)
        printf("込込\n");
    else
        n = f(n+1);

    return n;
}

int g(int m)
{
    m = m*2;
    return m;
}

int main(void)
{
    int val;

    val = f(5);

    return 0;
}

4-7 recursive implementation of the factorial

# include <stdio.h>

//假定n的值是1或大于1的值
long f(long n)
{
    if (1 == n)
        return 1;
    else
        return f(n-1) * n;

}

int main(void)
{
    printf("%ld\n", f(100));

    return 0;
}

Guess you like

Origin www.cnblogs.com/Guard9/p/11144974.html