The fibonacci
function (2 base cases, 2 recursive calls):
fibonacci(0) = 1 fibonacci(1) = 1 fibonacci(n) = fibonacci(n-2) + fibonacci(n-1) [for n>1]Let's compute
fibonacci(4)
:
fibonacci(4) = fibonacci(3) + fibonacci(2) = (fibonacci(2) + fibonacci(1)) + (fibonacci(1) + fibonacci(0)) = ((fibonacci(1) + fibonacci(0)) + 1) + (1 + 1) = ( 1 + 1 ) + 1) + (1 + 1) = 5
Structural definition of a Stack:
Recursive definitions are extremely common in Computer Science (e.g. Lists, Trees, etc).