Viii. recursion (recursion)

Source: Internet
Author: User
recursion (recursion)


The C-language function can make a recursive call (recursive called), which means that the function itself can be called within the function. When a function makes a recursive call, the variables it uses are stacked in the stacking area, and each time a return is executed, the variables used by the function in that layer call are returned from the stack. properties of L-l recursive function

1. (1) Each execution of a return statement, the scope of the problem narrowed

2. (2) having a condition for terminating the recursion

Program Examples : Recursive

int factorial(int j)

{
if (n==1)
return (1);
Else
Return (nfactorial(n-1));
}


void Main (void)
{
int i;
for (i=0; i< 5; i++)
printf ("%d! =%d/n ", I, factorial(i));
}

Result:
1! is 1
2! is 2
3! is 6
4! is 24

internal processing of L-l recursive function

1.1. Call of the general function

2.2. Call of recursive function

01

02

03

04

05

int list[6] = {1, 2, 3, 4, 5, 6}; /* Array Contents */

06

07

/* ----------------------------------------------------------------------------------- */

08

/* Recursive array inverse printing function */

09

/* ----------------------------------------------------------------------------------- */

10

void Invert_array (int j)

11

{

12

if (J < 6) &n

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.