Lecture 7
Lecture 7
Recursion
Muhammad Saqib Ilyas
Learning goals
• Describe the various memory segments and their
purpose
• Describe recursion in simple but correct terms
• Implement a recursive solution to a simple problem in C
• Visualize how a recursive program runs
Types of variables in C
• What types of variables (not data types) can you think
of?
• Local
• Global
• Static
Spot the local and global variables
int queue_size = 10; int main() {
void foo(int num) { queue_size++;
printf("%d\n", num); foo(3);
return 0;
int x = 0; }
{
int x = 1; What do you expect the output to
printf("%d\n", x); be?
}
printf("%d\n", x);
queue_size--;
}
Static variables
void bar() {
static int count = 0;
printf("%d\n", count);
count++;
} What do you expect the output to
int main() { be?
bar();
bar();
return 0;
}
Scope of a variable
• Portion of the program where the variable is accessible
• Local variables:
• Within the function or block where defined
• Global variables:
• Throughout the program
Lifetime of a variable
• When is a variable created?
• When is it destroyed?
• Local:
• Created: Each time the function is called
• Destroyed: When the function returns
• Local static / Global:
• Created: When the program starts
• Initialized: Once
• Destroyed: When the program ends
How does a computer handle all
this?
x86 memory layout
Local variables Stack n-1
Program
Text 0
instructions
Address
Recursion
• What is recursion?
• The smart-Alec way of answering a question
• Example: n! = n(n-1)!
Example dialog
You haven’t
answered the
What is
What is 2! = 2 x 1question It is 23x
4
2! = 2 x 1!
1!?
4!?
3!?
2!? =2 It 1!
is 1
2!
3!
3! = 3 x 2
3! = 3 x 2!
=6
4! = 4 x 6
4! = 4 x 3!
= 24
Recursion in code
• Recursive step: n! = n x (n – 1)!
• Base case: 1! = 1
n = 3;
printf(“%ld\n”, factorial(n));
Recursion in C
unsigned long factorial(unsigned
int n) {
if (n == 1) n=1
return 1;
unsigned longreturnfactorial(unsigned
n Recursive
* factorial(n – 1);
int n) { } 2 x 1 = call
if (n == 1) 2 n=2
return 1;
unsigned longn factorial(unsigned
return *Recursive
factorial(n – 1);
int n)}{ 3 x 2 = call
if (n == 1) 6 n=3
return 1;
return n * factorial(n – 1);
}
Recursion Implementation
• Each function call creates its own stack frame
• Has independent copies of local variables
• Recursive calls keep “stacking” these stack frames
fact(1)1
return
In this phase, we say
that “the stack is 2return
* fact(1)
2
unwinding”
winding”
3return
* fact(2)
6
Exercise – Exponentiation
Given positive integers a and n, write a Python program to
calculate an
https://bit.ly/
3XP7oCy
Product of two positive integers
• Given two positive integers a, and b, write a recursive C
function to calculate a x b.
Sum of digits
• Given a positive integer n, write a recursive C function
to find the sum of its digits.
• Example:
• If the input number is 123, then the answer should be 1 + 2 +
3=6
Binary search
• Given a sorted array, write a recursive function that
returns:
• The index of a target value in the array,
• -1 if that value is not found
Palindrome
• A palindrome is a string that reads the same in both
directions
• Examples:
• Dad
• DAAD
• Write a recursive function that returns:
• 1 if a string is a palindrome
• 0 otherwise
Maximum value in an array
• Given an array of integers, write a recursive function
that returns the maximum value in that array
Recap
• Local variables are allocated on the stack
• Dynamic variables are allocated on the heap
• Static variables have program lifetime and aren’t re-
initialized
• Recursion
• Represent a problem in terms of smaller versions of itself
• Divide and conquer:
• Keep breaking down a problem into smaller ones until it becomes trivial
• Systematically combine the results of smaller problems
• Recursive implementations are deceptively simple