Question:
What is recursion in Java and how does do we use it?
Omg
2011-02-15 17:27:12 UTC
I am learning Java on my own but i dont get what recursion is. I got some code that loops itself. Like a method looping itself till condition is false. Can someone please explain to me recursion really good.
Four answers:
anonymous
2011-02-15 17:43:16 UTC
Recursion is when a method calls itself. It is sometimes used because some problems are easier to solve using a recursive solution than an iterative solution. There is no problem that absolutely requires the use of recursion. Its just that for some problems it is easier.



Recursion is less efficient and uses more memory than iterative solutions. This is because each time the method calls itself, a new stack frame is created with that method, and all of its state. In severe cases, this can result in overflowing the stack, or running out of memory. Because of this, recursion does have to be used with care.



The classic example of a recursive function is using recursion to generate a fibonacci sequence:



long fib(int n) {

if (n == 0 || n == 1) {

return n;

} else {

return fib(n - 1) + fib(n - 2);

}

}
?
2016-10-15 07:37:37 UTC
From what it sounds like there is not any such element as finished, yet. look at this from the pc's perspective. You compiled it, meaning the pc reported it is familiar with the words you're using. then you fairly ran it. once you run java courses the 1st element apparently at is the key approach. So it starts off there and it examine the device.out.println(finished); it is familiar with you opt to print some thing yet so some distance because it is regularly occurring with it has no theory what finished is. It does not examine this methodology from suited to backside it purely follows what's accomplished interior the key approach. additionally i'm undecided why you're passing in finished into the function TotalArea it form of seems such as you purely exchange its fee, which you may desire to besides make it a variable interior the function not a parameter (the effect of the function isn't replaced via preliminary values of finished). And it additionally does not seem to be recursion, the function TotalArea in no way calls itself, that's the definition of recursion.
?
2011-02-15 17:50:10 UTC
Recursion is a function calling itself. Others have posted great examples. A lot of software position interviews require you to write a recursive function so learn this very well.
i8badhaggis
2011-02-15 17:32:45 UTC
//the start!

public static function main(){

factorialMethod(4);//calls function factorialMethod, passing the number 4

}



public function factorialMethod(int v){

if(v > 1){

v--;

return factorialMethod(v) * v;

}else{

return v;

}

}



alright so the main method calls method factorialMethod with the value of 4.



method factorialMethod calls itself with value of 3 then 2 then 1 and finally stops.

it then returns 1 which gets multiplied by 2 returning 2 which gets multiplied by 3 returning 6 which gets multiplied by 4 returning finally 24 back to the main method.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...