Understanding of parameters and variables in Recursion

Source: Internet
Author: User

Zookeeper

For recursive functions: the lifetime and call time of parameters and local variables

========================================================== ========================================================== ============================

# Include

Int binary_to_ascii (unsigned int value)
{
Unsigned int quotient;

Quotient = value/10; --------------------------------- the statement before recursive call
If (quotient! = 0)
Binary_to_ascii (quotient );

Putchar (value % 10 + '0'); ---------------------------------: recursive call statement

}
---------------------------------- Think about their call Sequence

How does recursion help us print these characters in the correct order? The following is the workflow of this function.
1. Divide the parameter value by 10
2. If the quotient value is non-zero, call binary-to-ascii to print the numbers of the quotient's current value.

3. Then, print the remainder of the division operation in step 1.

Note that in step 1, we need to print the numbers of the current quotient value. The problem we are facing is exactly the same as the original one, but the value of the variable quotient becomes smaller. We can solve this problem by using the function we just compiled (converting integers into various numeric characters and printing them out. Because the quotient value is getting smaller and smaller, recursion ends.

Once you understand recursion, the easiest way to read a recursive function is not to get stuck in its execution process, but to believe that it will successfully complete its tasks. If each step is correct, Your restrictions are set correctly, and each call is closer to the restrictions, recursive functions can always complete the task correctly.

However, to understand the working principle of recursion, You need to track the execution process of recursive calls, so let's do this. The key to tracking the execution process of a recursive function is to understand how the variables declared in the function are stored. When a function is called, its variable space is created on the runtime stack. Previously called function variables are left on the stack, but they are hidden by the new function variables, so they cannot be accessed.

This is the case when recursive functions call themselves. Each time a new call is made, a batch of variables will be created, and they will mask the variables created in the previous call of recursive functions. When tracking the execution process of a recursive function, you must differentiate the variables called for different scores to avoid confusion.

The function in the program has two variables: parameter value and local variable quotient. The following figures show the stack status. Currently, accessible variables are located at the top of the stack. All other called variables are dimmed, indicating they cannot be accessed by the currently executed function.
Suppose we call the recursive function with the value 4267. The stack content is shown in the following figure when the function is executed:


After division is executed, the stack content is as follows:


Then, the if statement determines that the quotient value is non-zero. Therefore, it executes a recursive call to this function. When this function is called for the second time, the stack content is as follows:

A batch of new variables are created on the stack to hide the previous batches of variables. They cannot be accessed unless the current recursive call returns. After the division operation is executed again, the stack content is as follows:


The quotient value is currently 42 and is still non-zero. Therefore, you need to execute recursive calls and create a number of variables. After the start operation of this call, the stack content is as follows:


A batch of new variables are created on the stack to hide the previous batches of variables. They cannot be accessed unless the current recursive call returns. After the division operation is executed again, the stack content is as follows:


The quotient value is currently 42 and is still non-zero. Therefore, you need to execute recursive calls and create a number of variables. After the start operation of this call, the stack content is as follows:


In this case, the quotient value is still non-zero and recursive call is still required. After the division operation, the stack content is as follows:


The statement itself is not called recursively. The statements executed so far only perform Division operations and test the quotient value. Because these statements are recursively called for repeated execution, their effects are similar to loops: When the quotient value is not zero, its value is used as the initial value to re-start the loop. However, recursive calls save some information (this is different from the loop), so it is better to save the variable value in the stack. This information will soon become very important.

Now that the quotient value is zero, the recursive function no longer calls itself, but begins to print the output. Then the function returns and destroys the variable values on the stack.

Each time putchar is called to obtain the last number of the variable value, the method is to perform the modulo 10 remainder operation on the value, and the result is an integer between 0 and 9. Add it to the character constant '0', and the result is the ASCII character corresponding to the number, and print it out.

Output 4:


Then the function returns, and its variables are destroyed from the stack. Next, the previous call of the recursive function continues execution. She uses her own variables, which are now at the top of the stack. Because its value is 42, the number printed after putchar is called is 2.

Output 42:


Then the call of the recursive function is returned, and its variables are also destroyed. At the top of the stack, the variable that the recursive function called again. Recursive call continues from this position. The number printed this time is 6. Before this call is returned, the stack content is as follows:

Output 426:


Now we have expanded the entire recursion process and returned to the initial call of the function. This call prints the number 7, that is, the remainder of its value parameter except 10.

Output 4267:


Then, this recursive function is completely returned to the location where other functions call it.
If you sort the printed characters one by one and appear on a printer or screen, you will see the correct value: 4267

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.