



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to recursive functions in computer science, explaining how a function can refer to itself at a smaller scale and the importance of base cases to prevent infinite recursion. It includes examples, formulas, and a recursive function definition, as well as notes on recursion programming and the creation of a call stack.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




CSCE150A
Recursive Functions
Lecture 11 – Recursion
Derrick Stolee
Spring 2009
1 / 5^ [email protected]
CSCE150A
Recursive Functions
Sometimes, a function can refer to itself at a different value, to show its relation to smaller cases. Example: The function f (n) = 2n^ can be referred to as doubling the previous value, f (n − 1).
f (n) = 2n^ = 2 · 2 n−^1 = 2 · f (n − 1). Taking the self-referential formula as a definition, we call this recursion.
CSCE150A
Recursive Functions
We can use case notation to define a recursive function:
f (n) =
1 if n = 0 2 · f (n − 1) otherwise. This outlines how we can translate from a function into a program: (^1) Identify inputs (^2) Test for base cases – Return if found (^3) Otherwise – Return evaluation of recursive formula
CSCE150A
Recursive Functions
int f(int n) { if ( n == 0 ) { return 1; }
return 2*f(n-1); }