About the basics of function recursion

Table of contents

what is recursion

 function recursion

 Function constraints

Give an example to explain the implementation of function recursion

topic

Question analysis

Idea analysis

Non-recursive:

recursion:

Implementation code:

topic

Question analysis

Idea analysis

Implementation code:

Stack overflow problem caused by function recursion


what is recursion

Recursion is to call itself directly or indirectly, simplifying a large and complex program into < /span> replace. simple programs programs that use Smaller

The purpose of recursion is to reducebig things into small ones.

 function recursion

occurs again in the process of callinga functiondirectly or indirectlyCall thefunction itself

One of the features of the C language is that it allows function recursion.

 Function constraints

The implementation of recursionrequires2 necessary conditions:

  • Recursion existsRestriction condition. When this restriction condition is met, recursion willnot continue a>.
  • After each recursion, it should be getting closer and closer to this restriction.

Give an example to explain the implementation of function recursion

topic

 There were 5 students sitting together and asked the 5th student how old he was. He said he was 2 years older than the 4th student. When asked how old the fourth student was, he said he was 2 years older than the third student. I asked the third student and he said he was 2 years older than the second student. Asked the second student and said he was 2 years older than the first student. Finally, I asked the first student and he said he was 10 years old. How old is the fifth student?

Question analysis

There are five students in total, numbered 1, 2, 3, 4, and 5. The first student is 10 years old, and each subsequent student is two years older than the previous student.

Idea analysis

Non-recursive:

If you want to calculate the age of the fifth student, that is

Second student: 10+2=12

The third student: 12+2=14

Fourth student: 12+2=16

Fifth student: 16+2=18

recursion:

The function we want to create: int age(int n), n is the serial number of the fifth student we require, and then n=5.

age(5)=age(4)+2

age(4)=age(3)+2

age(3)=age(2)+2

age(2)=age(1)+2

Same effect: age(n) = age(n-1)+2,Import:n=5

Implementation code:

Enter with n=5 for the first time,c=age(5-1)+2 -> c=age(4 )+2, call the age function again, the parameter is 4,

age(4)target returnc,c=age(4-1)+2-> c=age(3)+2, age function for repeated adjustment, number is 3,

Until it reachesage(1), age(1)=10, the recursion ends.

int age(int n)//5作为参数进来,n=5
{
    int c = 0;
    if(n==1)
        c = 10;
    else
        c=age(n-1)+2;//c=age(4)+2 这里age(4)参数为4,再次进入到 age 函数。
                     //函数age(4)又会走到这里,n=4,这里c=age(3)+2.
                     //c是函数的返回值,
    return (c);
}

limitation factor:

n=1

age(n) = age(n-1)+2

If our function is recursivewithoutrestrictions, it will fall intoInfinite loop.

sincen=1time,age(1)=10,Joining againc=age(n-1)+2

topic

Use recursive method to find n!

Question analysis

Thefactorial(factorial) of a positive integer is Theproduct of allpositive integers less than or equal to this number, and 0 The factorial of is 1. The factorial of a natural number n is written n!.

Idea analysis

5!=5x4x3x2x1

4!=4x3x2x1

3!=3x2x1

……

We found 5! =5x4!

4!=4x3!

Hence n! =nx(n-1)!

Implementation code:

 

#include <stdio.h>
int fact(int n)
{
    if(n==1)
        return 1;
    else
        return n*fact(n-1);
}
int main()
{
    int n = 0;
    scanf("%d",&n);
    int c = fact(n);
    printf("%d",c);
    return 0;
}

Stack overflow problem caused by function recursion

Every time function is called, it will apply for space in the memory stack area, Until the function returns the value, start to release space.

This space is mainly used tostore local variables in functions, and the function calling process Contextualinformation

We call this spaceFunction stack frame space.

The space we leave for the function stack frame islimited, so function recursion has requirements:

1. Restrictions

2. Getting closer and closer to our constraints

Otherwise, the function stack frame space will be opened up infinitely, resulting in stack overflow problems:

For our question of factorial of n just now, I changed the restriction condition to n=6. As the function runs, it will get further and further away from the restriction condition, which should be optimized. No result, under the msvc compiler, no error seems to be reported, and finally the run ends with

Summarize 

1. What is recursion?

2. Requirements for recursive restrictions

3. Solve the problem using recursion

4. Stack overflow problem (why there are restrictions)

Guess you like

Origin blog.csdn.net/2302_79491024/article/details/134300166