Recursive Function understanding

Source: Internet
Author: User
Code 1 . What is a recursive function)

A recursive function is a self-called function. It calls itself directly or indirectly within the function body, that is, the nested call of a function is the function itself.
For exampleProgramFor N ! :
Long FACT ( Int N)
{
If (N = 1 )
Return   1 ;
Return FACT (n - 1 ) * N; // function Self-called
}

2 Description of the function call Mechanism

No nested definition is allowed between any function. The called function and the called function are independent of each other (they can be called ). When a function is called, The called function protects the running environment and return address of the called function, so that the status of the called function can be completely restored after the called function returns a result, the status is not related to the called function.
Executed FunctionCodeAlthough it is the code body of the same function, because of the Call Point, call status, and different return points, it can be seen as a copy of the function and has nothing to do with the code that calls the function, therefore, the function code is independent. The stack space for running the called function is independent of the stack space for calling the function. Therefore, data between called functions is irrelevant. Functions are linked by parameter passing and return values. functions are black boxes.
This mechanism determines C/C ++ Supports recursive function calls.

3 . Recursive call form

There are two forms of recursive call: Direct recursive call and indirect recursive call.
Direct recursion means that the function itself is called in the function.
For example, the following code calculates the nth entry of the Fibonacci series. The first and second items of the Fibonacci series are 1, followed by the sum of the first two items, that is, 1, 1 , 2 , 3 , 5 , 8 , 13 ,.... Direct recursive call is used in the Code:
Long FIB ( Int X)
{
If (X > 2 )
Return (FIB (x - 1 ) + FIB (x - 2 )); // Direct Recursion
Else
Return   1 ;
}
Indirect recursive call refers to the call of other functions in the function, but other functions call the function again. For example, the following code defines two functions that constitute indirect recursive calls:
Int FNL ( Int A)
{
Int B;
B = FN2 ( + 1 ); // Indirect Recursion
// ...
}
Int FN2 ( Int S)
{
Int C;
C = FNL (S - 1 ); // Indirect Recursion
// ...
}
In the above example, the fn1 () function calls the FN2 () function, while the FN2 () function calls the fn1 () function again.

4 . Recursive Conditions

( 1 Must have a statement to complete the function task.
For example, the following code defines a recursive function:
# Include

Void Count ( Int Val) // Recursive functions do not return values.
{ If (Val > 1 )
Count (Val - 1 );,
Cout < " OK: "   <= "" This statement completes the function task = ""   />
The task of this function is displayed on the output device. " OK: integer ".
( 2 )-A test to determine whether recursive calls can be avoided
For example, in the Code of the previous example, the Statement " If (Val> 1) " It is a test. If the conditions are not met, recursive calls are not performed.
( 3 ) A recursive call statement.
The parameters of the recursive call statement should gradually approach those that do not meet the conditions, and finally cut off recursion.
For example, in the above Code, the statement" If (Val > 1 ) "Is a recursive call, and the parameters are getting smaller. This development trend can make the test " If (Val> 1) "is not satisfied.
( 4 ) First test, then recursively call.
In the definition of recursive functions, you must first test and then call them recursively. That is to say, recursive calls are conditional and can be recursive only when the conditions are met.
For example, the following code calls the function itself unconditionally, resulting in unrestricted recursion and eventually overflow of stack space:
# Include
Void Count ( Int Val)
{
Count (Val - 1 ); // Unrestricted Recursion
If (Val > 1 ) // This statement cannot be reached
Cout < " OK: "   < }

5 . Eliminate Recursion

Most recursive functions can be replaced by non-recursive functions. For example, the following code calculates the maximum common divisor of two integers A and B, which are defined by recursive and non-recursive functions respectively:
Long Gcdt ( Int A, Int B) // Recursive version
{
If ( % B = 0 )
Return B;
Return Gcdl (B, % B );
}
Long Gcd2 ( Int A, Int B) // Non-recursive version
{
Int Temp;
While (B ! = 0 )
{
Temp = A % B;
A = B;
B = Temp;
}
Return A;
}

Thinking: N ! Non-recursive functions.

6 . Recursive Evaluation

Recursion aims to simplify program design and make the program readable.
But recursion increases the system overhead. In terms of time, the execution of the call and the returned additional work will take up the CPU time. In terms of space, with each recursion, the stack memory occupies more than one part.
Although non-recursive functions are efficient, they are difficult to program and have poor readability.
Modern programming aims at good readability. With the continuous improvement of computer hardware performance, programs give priority to readability rather than efficiency in more cases. Therefore, recursive functions are encouraged to implement program ideas.

 

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.