So a recursive function is a function that calls itself until a terminating condition is reached.
Say I want to calculate a Fibonacci number. The series is: 1, 1, 2, 3, 5, 8...
There's a pretty simple way to do this recursively
1 2 3 4 5 6 7 8 9
// Pretty shoddy version that requires current and prev to be 1 when first called,
// but it proves the point.
int fib(int current, int prev, int count)
if ((count == 0) || (count == 1))
elsereturn fib(prev, (prev + current), (count - 1));
So all you really need to figure out are:
- What the conditions of exiting are
- What variables the function needs to pass to itself to keep recursng.
The rest should just be a few conditional statements