📖 Scenario: Imagine you want to count down from a number to zero, like counting steps while walking down stairs.
🎯 Goal: You will build a simple recursive function in Go that counts down from a given number to zero, showing how recursion stops with a base case and continues with a recursive case.
📋 What You'll Learn
Create a function called countDown that takes an integer n.
Use a base case to stop recursion when n is less than 0.
Use a recursive case to call countDown with n-1.
Print the current value of n before the recursive call.
Call countDown with the starting number 3 in main.
💡 Why This Matters
🌍 Real World
Recursion is used in many real-world problems like navigating folders, solving puzzles, and processing tree structures.
💼 Career
Understanding recursion helps in coding interviews and solving complex problems efficiently in software development.
Progress0 / 4 steps
1
Create the main function with a call to countDown
Write a main function that calls countDown with the number 3.
DSA Go
Hint
The main function is the program's starting point. Call countDown(3) inside it.
2
Define the countDown function with a base case
Define a function called countDown that takes an integer n. Add a base case that returns immediately if n is less than 0.
DSA Go
Hint
The base case stops the recursion. Use if n < 0 { return }.
3
Add printing and recursive call in countDown
Inside countDown, print the value of n using fmt.Println(n). Then call countDown recursively with n-1.
DSA Go
Hint
Print n before calling countDown(n - 1) to count down step by step.
4
Run the program and print the countdown
Run the program and print the countdown from 3 to 0 using the countDown function.
DSA Go
Hint
Run the program. It should print numbers 3, 2, 1, 0 each on a new line.
Practice
(1/5)
1. What is the purpose of the base case in a recursive function?
easy
A. To print the result of the recursion
B. To call the function again with a smaller input
C. To increase the input size for the next call
D. To stop the recursion and prevent infinite calls
Solution
Step 1: Understand recursion basics
A recursive function calls itself repeatedly until a stopping condition is met.
Step 2: Identify the role of base case
The base case stops further recursive calls to avoid infinite loops.
Final Answer:
To stop the recursion and prevent infinite calls -> Option D
Quick Check:
Base case = stop recursion [OK]
Hint: Base case always stops recursion to avoid infinite loops [OK]
Common Mistakes:
Confusing base case with recursive case
Thinking base case calls function again
Ignoring base case leads to infinite recursion
2. Which of the following is the correct syntax for a base case in Go recursion to stop when n is zero?
easy
A. if n == 0 { return 0 }
B. if n = 0 { return 0 }
C. if n != 0 { return 0 }
D. if n > 0 { return 0 }
Solution
Step 1: Check Go syntax for condition
In Go, equality check uses '==' not '=' which is assignment.
Step 2: Identify correct base case condition
Base case stops recursion when n equals zero, so 'if n == 0 { return 0 }' is correct.
Final Answer:
if n == 0 { return 0 } -> Option A
Quick Check:
Use '==' for comparison in base case [OK]
Hint: Use '==' for comparison in base case conditions [OK]
Common Mistakes:
Using '=' instead of '==' for comparison
Wrong condition logic for base case
Returning wrong value in base case
3. What will be the output of this Go recursive function when called with factorial(3)?
func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n - 1)
}