📖 Scenario: Imagine you want to find the factorial of a number, which means multiplying that number by every smaller number down to 1. This is like stacking boxes: to get the total height, you add the height of the top box to the height of all boxes below it. Recursion helps us solve this by calling the same process again and again with smaller numbers.
🎯 Goal: You will build a simple recursive function in Go to calculate the factorial of a number. You will see how the function calls itself and how the answers build up step by step.
📋 What You'll Learn
Create a recursive function called factorial that takes an integer n and returns an integer.
Use a base case where if n is 1, the function returns 1.
In the recursive case, the function should return n * factorial(n-1).
Call the factorial function with the value 5 and store the result in a variable called result.
Print the value of result.
💡 Why This Matters
🌍 Real World
Recursion is used in many places like calculating combinations, navigating file systems, and solving puzzles like mazes.
💼 Career
Understanding recursion helps in writing clean code for complex problems and is often asked in coding interviews.
Progress0 / 4 steps
1
Create the factorial function with base case
Write a function called factorial that takes an integer n and returns an integer. Inside the function, if n equals 1, return 1.
DSA Go
Hint
Start by writing the function header and the base case that stops the recursion.
2
Add the recursive case to factorial function
Inside the factorial function, after the base case, add the recursive case that returns n * factorial(n - 1).
DSA Go
Hint
Multiply n by the result of calling factorial with n - 1.
3
Call factorial function with 5 and store result
In the main function, call factorial with the value 5 and store the result in a variable called result.
DSA Go
Hint
Use result := factorial(5) inside main.
4
Print the factorial result
Add a print statement in the main function to display the value of result.
DSA Go
Hint
Use fmt.Println(result) to print the factorial.
Practice
(1/5)
1. What happens when a recursive function calls itself without a stopping condition?
easy
A. It converts into a normal loop automatically
B. It immediately returns without doing anything
C. It runs only once and stops
D. It causes an infinite loop and eventually a stack overflow error
Solution
Step 1: Understand recursion without base case
Without a stopping condition, the function keeps calling itself endlessly.
Step 2: Effect on call stack
Each call adds a new frame to the call stack, which grows until memory runs out causing stack overflow.
Final Answer:
It causes an infinite loop and eventually a stack overflow error -> Option D
Quick Check:
Missing base case = infinite recursion [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Thinking recursion stops automatically
Confusing recursion with loops
Ignoring stack overflow possibility
2. Which of the following is the correct syntax to define a recursive function in Go that calculates factorial of n?
easy
A. func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) }
B. func factorial(n int) { if n == 0 { return 1 } return n * factorial(n-1) }
C. func factorial(n int) int { if n == 0 { return 0 } return n * factorial(n-1) }
D. func factorial(n int) int { if n == 1 { return 0 } return n * factorial(n-1) }
Solution
Step 1: Check function signature and return type
Factorial returns an int, so function must return int type.
Step 2: Verify base case and recursive call
Base case is n == 0 returning 1; recursive call multiplies n by factorial(n-1).
Final Answer:
func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } -> Option A
Quick Check:
Correct base case and return type = func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } [OK]