# How does the recursion here work?

Filed under:
|
|
|
##### java

code 1:

``````public static int fibonacci (int n){
if (n == 0 || n == 1) {
return 1;
} else {
return fibonacci (n-1) + fibonacci (n-2);
}

}
``````

how can you use fibonacci if you haven't gotten done explaining what it is yet? I've been able to understand using recursion in other cases like this:

code two:

``````class two
{
public static void two (int n)
{
if (n>0)
{
System.out.println (n) ;
two (n-1) ;
}
else
{
return ;
}
}

public static void main (String[] arg)
{
two (12) ;
}
}
``````

In the case of code 2 though n will eventualy reach a point at which it doesnt satisfy n>0 and the method will stop calling itself recursivly. in the case of code 2 though i don't see how it would be able to get itself from 1 if n=1 was the starting point to 2 and 3 and 5 and so on. Also i don't see how the line `return fibonacci (n-1) + fibonacci (n-2) would work since fibbonacci n-2 has to contain in some sense fibonacci n-1 in order to wrok but it isn't there yet.

I know my question is worded poorly but looking at this is making my mind explode. the book i'm looking at says it will work.

how does it work? `

© Stack Overflow or respective owner

• #### tail recursion vs. forward recursion

as seen on Stack Overflow - Search for 'Stack Overflow'
Can someone give me the difference between these two kinds recursions and example? specifically in ocaml. Thanks >>> More

• #### Are there advantages for using recursion over iteration - other than sometimes readability and elegance?

as seen on Programmers - Search for 'Programmers'
I am about to make two assumptions. Please correct me if they're wrong: There isn't a recursive algorithm without an iterative equivalent. Iteration is always cheaper performance-wise than recursion (at least in general purpose languages such as Java, C++, Python etc.). If it's true that recursion… >>> More

• #### Python recursive function error: "maximum recursion depth exceeded"

as seen on Stack Overflow - Search for 'Stack Overflow'
I solved Problem 10 of Project Euler with the following code, which works through brute force: def isPrime(n): for x in range(2, int(n**0.5)+1): if n % x == 0: return False return True def primeList(n): primes = [] for i in range(2,n): if isPrime(i): … >>> More

• #### Dynamic Programming Recursion and a sprinkle of Memoization

as seen on Stack Overflow - Search for 'Stack Overflow'
I have this massive array of ints from 0-4 in this triangle. I am trying to learn dynamic programming with Ruby and would like some assistance in calculating the number of paths in the triangle that meet three criterion: You must start at one of the zero points in the row with 70 elements. Your… >>> More

• #### How does the recursion here work?

as seen on Stack Overflow - Search for 'Stack Overflow'
code 1: public static int fibonacci (int n){ if (n == 0 || n == 1) { return 1; } else { return fibonacci (n-1) + fibonacci (n-2); } } how can you use fibonacci if you haven't gotten done explaining what it is yet? I've been able to understand… >>> More

• #### openss7 help help help

as seen on Stack Overflow - Search for 'Stack Overflow'
Hello, is there anyone in this forum who have experience with openss7 before ?. Or maybe stil developing with openss7 ?. I need to discuss something, since it's difficult to find the solution... >>> More

• #### Missing help files for Microsoft.WindowsMobile in Visual Studio 2008 help system

as seen on Stack Overflow - Search for 'Stack Overflow'
I've just installed the Windows Mobile 6.5.3 DTK, both standard and professional. Before that I had the standard and professional Windows Mobile 6 SDKs. All Windows Mobile help pages are missing in Visual Studio 2008's help system - in particular everything in the Microsoft.WindowsMobile namespace… >>> More

• #### Maven: Cannot get the help goals working (clean:help, compiler:help, etc)

as seen on Stack Overflow - Search for 'Stack Overflow'
Hi, I am new in Maven. Do you know what am I doing wrong below? Thanks SirFabel mvn -e clean:help Warning: JAVA_HOME environment variable is not set. + Error stacktraces are turned on. [INFO] Scanning for projects... [INFO] Searching repository for plugin with prefix: 'clean'. [INFO]… >>> More