Recall & Review
beginner
What is a recursive function?
A recursive function is a function that calls itself to solve smaller parts of a problem until it reaches a simple case it can solve directly.
Click to reveal answer
beginner
What is the base case in recursion?
The base case is the simplest situation in a recursive function where the function stops calling itself to avoid infinite loops.
Click to reveal answer
intermediate
How does recursion relate to breaking down problems?
Recursion breaks a big problem into smaller, similar problems, solves each smaller problem, and combines the results to solve the original problem.
Click to reveal answer
intermediate
What happens if a recursive function has no base case?
Without a base case, the function keeps calling itself forever, causing a stack overflow error or program crash.
Click to reveal answer
beginner
Example: What does this R function do?
factorial <- function(n) {
if (n == 0) return(1)
else return(n * factorial(n - 1))
}This function calculates the factorial of a number n by multiplying n by the factorial of n-1 until it reaches 0.
Click to reveal answer
What is the purpose of the base case in a recursive function?
✗ Incorrect
The base case stops the recursion by providing a simple answer, preventing infinite calls.
In R, how does a recursive function call itself?
✗ Incorrect
A recursive function calls itself by using its own name inside its definition.
What will happen if a recursive function lacks a base case?
✗ Incorrect
Without a base case, recursion never stops, causing a stack overflow error.
Which problem is a good fit for recursion?
✗ Incorrect
Factorial calculation naturally breaks down into smaller factorial problems, perfect for recursion.
What does this R code return?
factorial(3)
✗ Incorrect
factorial(3) = 3 * factorial(2) = 3 * 2 * factorial(1) = 3 * 2 * 1 = 6.
Explain how a recursive function works using a real-life example.
Think about how you might solve a big task by doing smaller similar tasks first.
You got /3 concepts.
Describe why a base case is important in recursive functions.
What happens if the function never stops calling itself?
You got /3 concepts.