0
0
R Programmingprogramming~10 mins

Recursive functions in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Recursive functions
Call function with n
Check base case: n == 1?
YesReturn 1
No
Call function with n-1
Wait for result
Multiply n by result
Return result
Previous call resumes
Final result returned
The function calls itself with a smaller input until it reaches the base case, then returns and combines results back up.
Execution Sample
R Programming
factorial <- function(n) {
  if (n == 1) {
    return(1)
  } else {
    return(n * factorial(n - 1))
  }
}
factorial(3)
Calculates factorial of 3 by calling itself with smaller numbers until 1.
Execution Table
StepCallnCondition (n==1?)ActionReturn Value
1factorial(3)3NoCall factorial(2)
2factorial(2)2NoCall factorial(1)
3factorial(1)1YesReturn 11
4factorial(2)2NoCalculate 2 * 12
5factorial(3)3NoCalculate 3 * 26
💡 Reached base case at n=1, then returned results back up the call stack.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
n332123
Return Value126
Key Moments - 3 Insights
Why does the function call itself with n-1 instead of just looping?
The execution_table shows at Step 1 and 2 that the function calls itself with smaller n to break the problem into simpler parts until the base case is reached.
What stops the recursion from going forever?
Step 3 in the execution_table shows the base case condition n==1 returns 1, stopping further recursive calls.
How does the function combine results from recursive calls?
Steps 4 and 5 show the function multiplies n by the returned value from the smaller call, building the final result back up.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value at Step 3?
A3
B2
C1
D6
💡 Hint
Check the 'Return Value' column at Step 3 where n=1 and base case returns 1.
At which step does the function first check the base case condition?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Condition (n==1?)' column to find when it is 'Yes'.
If we call factorial(4), how many total calls will appear in the execution_table?
A3
B4
C5
D6
💡 Hint
Each call reduces n by 1 until 1, so count calls from 4 down to 1.
Concept Snapshot
Recursive functions call themselves with smaller inputs.
They must have a base case to stop.
Each call waits for the smaller call's result.
Results combine as calls return.
Example: factorial(n) = n * factorial(n-1) with base case factorial(1)=1.
Full Transcript
This example shows how a recursive function works by calling itself with smaller inputs until it reaches a base case. The function factorial calls itself with n-1 until n equals 1. At that point, it returns 1. Then each previous call multiplies n by the returned value, building the final result. The execution table tracks each call, the condition check, and the return values step-by-step. The variable tracker shows how n and return values change during execution. Key moments clarify why recursion calls itself, how it stops, and how results combine. The quiz tests understanding of these steps.