1. A function is a self-called function. It calls itself directly or indirectly within the function body, that is, the function itself is nested. For example, the function fact (int n) in the following program is to evaluate n! # Include "stdio. H" # Include "conio. H" Long fact (int n) { If (n = 1) Return 1; Return (fact (n-1) * N; }Main () { Int n = 0; Long factofn = 0; Printf ("enter a positive integer/N "); Scanf ("% d", & N ); Factofn = fact (N ); Printf ("n! = % LD/N ", factofn ); } ① It doesn't matter if you don't understand the above program. Here is an example, for example, 3 !, In the first execution of the function fact, since 3 is not equal to 1, return (fact (n-1) * n is executed, that is, return fact (2) * 3; because fact (2) is unknown, we call fact (2) to obtain its value; ② For fact (2), n = 2. Because 2 is not equal to 1, return (fact (n-1) * n is executed, that is, return fact (1) * 3; Because fact (1) is unknown, fact (1) is called to obtain its value; ③ For fact (1), n = 1. Because 1 is not equal to 1, return 1 is executed, so fact (1) = 1; ④ Because fact (1) = 1, fact (1) * 2 in step 2 is equal to 2, so fact (2) = 2; ⑤ Because fact (2) = 2, fact (2) * 3 in step 1 can be obtained, and it is 6; 6. The final result is fact (3) = 6. 2. Recursive Conditions 1. There are conditions for Recursive termination. Generally, an if judgment statement is used. For example, n is obtained above! In the program, the statement "If (n = 1)" is a recursive termination condition. If its condition is met, recursion is terminated. 1. a recursive call statement. The recursive call statement should call itself, and the function parameters should gradually approach the conditions for Recursive termination. For example, in the above Code, "Return (fact (n-1) * n;" is a recursive call statement. It calls itself and the value of the parameter gradually decreases. The tendency of this parameter will eventually satisfy the recursive termination condition "If (n = 1)", thus terminating the recursive call. 3. Execute the test code to test whether the conditions for Recursive termination are met, and then execute the code for recursive calls. In the definition of recursive functions, you must first test and then call them recursively. That is to say, recursive calls enable recursion only when conditional conditions are met. For example, the following code calls the function itself unconditionally, resulting in unrestricted recursion, forming an endless loop. Long fact (int n) { Return (fact (n-1) * N; If (n = 1) Return 1; } 3. How to quickly and correctly write recursive functions. From the above conditions, writing a recursive function is very stylized. As long as you follow certain rules, you can easily write a recursive function. There are three steps to write a recursive function: ① Write the iteration formula; ② Determine the conditions for Recursive termination; ③ Translate ① into code. Still want n! For example: ① Write the iteration formula: n! The iteration formula of isDownload(3.31 KB) ② Determine the recursion termination condition: 1! = 1 is the recursion termination condition. ③ Translate ① into code: Write the formula on the right of the iteration formula equal sign into the return statement, that is, return (fact (n-1) * N; Set 1! = 1 translated into a judgment statement: If (n = 1) return 1; Write the code according to the principle of first test and then recursion. Long fact (int n) { If (n = 1) Return 1; Return (fact (n-1) * N; } The following is another example, hoping to help you further understand this principle. Write a function and calculate: F (n) = 1 + 2 + 3 + ...... + N Value ① Write the iteration formula: the iteration formula isDownload(2.87 KB)
② Determine the recursion termination condition: F (1) = 1 is the recursion termination condition ③ Translate ① into code: Write the formula on the right of the equal sign of the iteration formula into the return statement, that is, return (sum (n-1) + N; Set 1! = 1 translated into a judgment statement: If (n = 1) return 1; Write the code according to the principle of first test and then recursion. Long sum (int n) { If (n = 1) Return 1; Return (sum (n-1) + N; }
|