Recursive (c + +) (GO)

Source: Internet
Author: User

1. What are recursive functions (recursive function)

A recursive function is a self-invoking function that calls itself directly or indirectly in the body of a function, that is, the function's nested invocation is the function itself.
For example, the following program is for n!:
    

  



2. Description of the function invocation mechanism

No definition can be nested between functions, and the calling function and the called function are independent from each other (can be called from each other). When a function call occurs, the running environment and return address of the calling function are protected in the called function so that the state of the calling function can be fully recovered after the function is returned, and the state is independent of the function being tuned.
The code that runs the function is the code body of the same function, but because of the call point, the call state, the return point is different, can be regarded as a copy of the function, and the code called the function is independent, so the code of the function is separate. The stack space in which the function is run is independent of the stack space of the calling function, so the data between the calling function is irrelevant. Functions are linked by parameter passing and return values, and functions are seen as black boxes.
This mechanism determines that C + + allows recursive invocation of functions.

3. The form of a recursive call

Recursive invocation has two forms: direct recursive invocation and indirect recursive invocation.
Direct recursion is the invocation of the function itself in the function.
For example, the following code asks for the nth term of the Fibonacci sequence. The first and second entries of the Fibonacci sequence are 1, and each subsequent entry is the sum of the first two items, namely 1,1,2,3,5,8,13,...。 Direct recursive calls are used in the code:
   

  


Indirect recursive invocation refers to the invocation of other functions in a function, which in turn calls this function. For example, the following code defines two functions, which constitute an indirect recursive invocation:
    

  


In the previous example, the FN1 () function called the FN2 () function, and the FN2 () function called the FN1 () function.

4. Conditions of recursion

(1) must have a statement to complete the function task.
For example, the following code defines a recursive function:
    

  


The task of this function is to display "OK: integer value" on the output device.
(2)-a test that determines whether recursive calls can be avoided
For example, in the code in the example above, the statement "if (val>1)" is a test that does not make a recursive call if the condition is not met.
(3) A recursive call statement.
The parameters of the recursive call statement should be approximated to meet the condition, and eventually break the recursion.
For example, in the above code, the statement "if (val>1)" is a recursive call, the parameters are getting smaller, this trend can make the test "if (val>1)" ultimately not satisfied.
(4) test first, then recursive call.
In the definition of a recursive function, it must be tested before recursive invocation. In other words, recursive invocation is conditional and satisfies the condition before it can be recursive.
For example, the following code unconditionally invokes the function itself, causing unrestricted recursion, which eventually overflows the stack space:
   

  


5. Eliminate recursion

Most recursive functions can be replaced by non-recursive functions. For example, the following code asks for a greatest common divisor of two integers, a, b, defined by a recursive and non-recursive function, respectively:
    

  


Thinking: The recursive function of n! is not recursive.

6. Evaluation of recursion

The purpose of recursion is to simplify the programming and make the program easy to read.
But recursion increases the overhead of the system. Time, the execution of calls and the return of extra work takes up CPU time. Space, with each recursive, stack memory occupies a little more.
The corresponding non-recursive functions are highly efficient, but they are more difficult to program, and are relatively poor in readability.
The goal of modern programming is mainly readability. With the continuous improvement of computer hardware performance, the program is preferred to be readable rather than efficient in more situations, so it is encouraged to implement the program idea with recursive function.

(Transferred from Http://www.cnblogs.com/seaven/archive/2010/12/17/1908953.html)

Recursive (c + +) (GO)

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.