Recursive Definitions and Function Calls Group1
Recursive Definitions and Function Calls Group1
1. Recursive Definitions
Recursion is a fundamental concept in computer science where a function calls itself to solve a problem
by breaking it down into smaller, more manageable subproblems. Recursive definitions consist of two
key parts:
Base Case (Anchor/Ground Case) : The simplest case that can be solved directly without further
recursion.
Inductive Step : A rule or set of rules that reduces the problem to a simpler version of itself.
n!={
n⋅(n−1)!
if n=0
if n>0
if (n == 0) {
} else {
F(n)=
F(n−1)+F(n−2)
if n=0
if n=1
if n>1
if (n == 0) {
} else {
When a function is called recursively, the system uses a runtime stack to manage the state of each
function call. Each recursive call creates a new activation record (or stack frame) on the runtime stack,
which contains:
Local variables.
Since n=3
Since n=2
Since n=1
Since n=0, the base case is reached, and the function returns 1.
As each activation record is popped off the stack, the results are combined:
factorial(0) returns 1.
Here’s how the runtime stack looks during the execution of factorial(3):
Here’s how the runtime stack looks during the execution of factorial(3):
STACK FRAME
PARAMETERS
LOCAL VARIABLES
RETURN ADDRESS
factorial(3)
n=3
factorial(2)
n=2
factorial(1)
n=1
factorial(0)
n=0
When factorial(0) returns, the stack unwinds, and each frame is popped off as the results are computed
3. Activation Records
Parameters and Local Variables : Values passed to the function and local variables declared within it.
Return Address : The memory address where execution resumes after the function returns.
Parameters: n = 3
Dynamic Link: Pointer to the activation record of the caller (e.g., main())
As each recursive call is made, a new activation record is created, and the stack grows. When the base
case is reached, the stack starts to unwind, and the results are propagated back up the call chain.