J2SE Study Notes (1)--recursive function

Source: Internet
Author: User

What is a 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.

Example Analysis

The second number is the sum of the first two numbers, and the 40th number

public class Fab2 {public static void main (String arg[]) {System.out.println (f (40));} public static int F (int n) {if (N==1 | | n==2) {return 1;} else {return F (n-1) + f (n-2);}}}

Operation Result:



The procedure for invoking this function is described below, for the sake of simplicity, F (5) as an example

Non-recursive functions to achieve the above functions

public class Fab{public static void Main (string[] args) {System.out.println (F (5));} public static long F (int index) {if (Index < 1) {System.out.println ("Wrrong"); return-1;} if (index = = 1 | | index = = 2) {return 1;} Long F1 = 1;long F2 = 1;long f = 0;for (int i = 0; I <index-2; i++) {f = f1 + F2;f1 = F2;f2 = f;} return f;}}

Run Results




characteristics of Recursive functions

(1) The original problem is transformed into a new problem with the same solution method

(2) The size of the new problem is smaller than the original problem

(3) New problems can be translated into new, smaller problems with the same solution, directly to the termination conditions


recursive function conditions

(1) The existence of the recursive end condition and the value at the end

(2) can be expressed in recursive form, and the development of recursive termination conditions


recursive reflection of thinking


That is, punches, trivial.


comparison of recursive function and non-recursive function

(1) 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, the stack

Memory takes a little more.

(2) The corresponding non-recursive function is efficient, but it is more difficult to program, and relatively poor readability.

The goal of modern programming is mainly readability. With the continuous improvement of computer hardware performance, the program is preferred to read in more situations than

Efficient, so the recursive function is encouraged to implement the program idea.



J2SE Study Notes (1)--recursive function

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.