0
0
R Programmingprogramming~5 mins

Recursive functions in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ATo stop the recursion and prevent infinite calls
BTo call the function again
CTo make the function run faster
DTo print the result
In R, how does a recursive function call itself?
ABy using its own name inside the function
BBy using a loop
CBy calling another function
DBy using a variable
What will happen if a recursive function lacks a base case?
AIt will return zero
BIt will run forever and cause an error
CIt will run once and stop
DIt will print a message
Which problem is a good fit for recursion?
AReading a file
BAdding two numbers
CPrinting a message once
DCalculating factorial of a number
What does this R code return?
factorial(3)
A9
B3
C6
D1
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.