UMBC CS 201, Fall 05
Recursion
Definition
recursion n. See recursion.
Recursion in Mathematics
A recursive definition is one which refers to itself as in these
definitions of the factorial and
fibonacci functions.
n! or fact(n) is defined as:
fact(0) = 1
fact(n) = n * fact(n-1) , for n > 0
the nth fibonacci number or fib(n) is defined as:
fib(0) = 1
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2) , for n > 1
Recursion in Programs
A recursive function is a function that calls itself.
- Each time the function calls itself, it gets a new set of
parameters and local variables.
- The parameters and local variables from a previous call to the
recursive function will still be preserved until the function returns
from the previous call.
The Factorial Function in C
int Factorial (int n)
{
if(n == 0)
{
return (1);
}
else
{
return (n * Factorial(n - 1));
}
}
CSEE
|
201
|
201 F'05
|
lectures
|
news
|
help
Last Modified - Saturday, 05-Nov-2005 11:22:37 EST