D. Pin D. Pin - 3 years ago 329
Pascal Question

Can a recursive method have more than one base case in Pascal?

I was wondering if you can have more than just one base case on a recursive procedure/function in Pascal.
If so, can you please give me a simple example? And please explain why this is possible?

Answer Source

The simple Fibonacci sequence has two base cases:

f(0) = 0
f(1) = 1
f(n) = f(n - 1) + f(n - 2)

And, of course, you can write it in Pascal:

function Fib(n: integer): integer;
  if n = 0 then Fib := 0
  else if n = 1 then Fib := 1
  else Fib := Fib(n - 1) + Fib(n - 2)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download