Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: RECURSION

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default RECURSION

    Could someone explain recursion to me please. I get that a function loops coz it is called several times but I can't get why and how! Frustration.


  2. #2
    Junior Member
    Join Date
    Apr 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: RECURSION

    You are probably missing an exit condition. Show the codes you have.
    In a nutshell:
    if condition is met
        return
    else
        call function again

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: RECURSION

    Thread moved from "Whats wrong with my code?"

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: RECURSION

    Recursion simply means recurring again and again. In another word, it is function calling the function itself until certain condition is met. For example:- If you look at the Fibonacci number(fibonacci number is sequence of number where the next number is the addition of past two numbers). The fibonacci numbers are 0,0,1,1, 2, 3, 5, 8. 8 is obtained by adding 5 and 3, 5 is obtained by adding 2 and 3 and so on. This can be generated easily by recursion.
    Anything that can be done with recursion can also be done iteratively or without using recursion. When you use recursion it keeps your code short. However, in java it is advisable not to use recursion because of the performance and memory issue. But it doesn't mean recursion is bad. In other programming language like LISP, recursion is the best way of solving problems. Don't bother too much about it. Just learn its basics and how it works. You will hardly use recursion in future if you continue in java.

  5. The Following User Says Thank You to myjava For This Useful Post:

    kevthanewversi (April 21st, 2013)

  6. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: RECURSION

    Quote Originally Posted by myjava View Post
    Recursion simply means recurring again and again. In another word, it is function calling the function itself until certain condition is met. For example:- If you look at the Fibonacci number(fibonacci number is sequence of number where the next number is the addition of past two numbers). The fibonacci numbers are 0,0,1,1, 2, 3, 5, 8. 8 is obtained by adding 5 and 3, 5 is obtained by adding 2 and 3 and so on. This can be generated easily by recursion.
    Anything that can be done with recursion can also be done iteratively or without using recursion. When you use recursion it keeps your code short. However, in java it is advisable not to use recursion because of the performance and memory issue. But it doesn't mean recursion is bad. In other programming language like LISP, recursion is the best way of solving problems. Don't bother too much about it. Just learn its basics and how it works. You will hardly use recursion in future if you continue in java.
    Thanks a lot man and how does execution go about when the method gets called again? The flow of execution.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: RECURSION

    Each call to the method is the same as any other call to any other method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Recursion
    By maple1100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2013, 01:49 AM
  2. [SOLVED] Recursion help
    By Actinistia in forum Java Theory & Questions
    Replies: 3
    Last Post: March 21st, 2011, 12:26 PM
  3. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  4. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 11:50 PM