📖 Scenario: Imagine you have a stack of boxes, and you want to find out how many boxes are in the stack by taking one box off at a time until none are left. This is similar to how recursion works in programming, where a function calls itself to solve smaller parts of a problem.
🎯 Goal: You will write a simple recursive function in C to count down from a number to zero, printing each step. This will help you understand how recursion works and how the call stack grows and shrinks.
📋 What You'll Learn
Create a recursive function called countdown that takes an integer parameter n.
The function should print the current value of n.
If n is greater than zero, the function should call itself with n - 1.
Create a variable start with the value 5 to begin the countdown.
Call the countdown function with start.
Print the message "Countdown complete!" after the recursion finishes.
💡 Why This Matters
🌍 Real World
Recursion is used in many real-world problems like searching files in folders, solving puzzles, and processing tree structures.
💼 Career
Understanding recursion is important for software developers, especially when working with algorithms, data structures, and problem-solving.
Progress0 / 4 steps
1
Create the countdown function declaration
Write the function declaration for void countdown(int n) in C. Inside the function, add a printf statement to print the current value of n in the format "%d\n".
DSA C
Hint
Use void countdown(int n) to declare the function. Use printf("%d\n", n); to print the number.
2
Add the recursive call inside countdown
Inside the countdown function, add an if statement that checks if n is greater than zero. If true, call countdown(n - 1) to continue the countdown.
DSA C
Hint
Use if (n > 0) to check the condition, then call countdown(n - 1); inside the block.
3
Create the start variable and call countdown
In the main function, create an integer variable called start and set it to 5. Then call the countdown function with start as the argument.
DSA C
Hint
Declare int start = 5; and call countdown(start); inside main.
4
Print completion message after recursion
After calling countdown(start) in main, add a printf statement to print "Countdown complete!\n".
DSA C
Hint
Use printf("Countdown complete!\n"); after the recursive call.
Practice
(1/5)
1. What happens when a recursive function calls itself without a base case?
easy
A. The function converts into a loop automatically.
B. It causes infinite recursion and eventually a stack overflow error.
C. The function runs only once and stops.
D. The function returns immediately without doing anything.
Solution
Step 1: Understand the role of base case in recursion
A base case stops the recursion by providing a condition to end the repeated calls.
Step 2: Analyze what happens without a base case
Without a base case, the function keeps calling itself endlessly, filling the call stack until it overflows.
Final Answer:
It causes infinite recursion and eventually a stack overflow error. -> Option B
Quick Check:
No base case = infinite recursion [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Thinking recursion stops automatically without base case
Confusing recursion with loops
Assuming function returns without base case
2. Which of the following is the correct syntax to define a recursive function in C that calculates factorial?
easy
A. int factorial(int n) { if (n == 1) return 0; else return n + factorial(n - 1); }
B. int factorial(int n) { while (n > 0) return n * factorial(n - 1); }
C. int factorial(int n) { if (n == 0) return 0; else return n * factorial(n + 1); }
D. int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }
Solution
Step 1: Check base case correctness
Factorial base case is when n == 0, return 1.
Step 2: Check recursive call and operation
Recursive call should be factorial(n - 1) multiplied by n.
Final Answer:
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } -> Option D
Quick Check:
Base case n==0 and multiply with factorial(n-1) [OK]
Hint: Base case returns 1; recursive call uses n-1 [OK]