
Hello again, Stephen! I'm not exactly clear about what you're asking, but I'm guessing you're asking one of two questions (or both) I've answered below.
Q1) What's the mathematical operation we're performing?
The calculate_sum() function calculates the sum from 1 to n, where n is the argument passed to it.
Here, n = 3, so calculate_sum() should return:
calculate_sum(3) = 1 + 2 + 3 = 6==============================================
Q2) How does this recursive function work?
Recursion is a confusing topic that even many of us still struggle with from time to time.
What's important to remember about recursion is this:
The recursive function doesn't start returning values until the base case is reached.
In our case, the base case is n == 1.
So, calculate_sum(3) will recursively call itself until calculate_sum(1) is called.
Once we've reached this base case, calculate_sum(1) will return a value to calculate_sum(2), which in turn returns a value to calculate_sum(3).
Here's the sequence of the recursive calls:
calculate_sum(3) --->calculate_sum(2) --->calculate_sum(1)
Here's the sequence of value returns:
calculate_sum(1) --->calculate_sum(2) --->calculate_sum(3)
I'll try to explain it to the best of my abilities. Please try to read this explanation alongside the image we've provided in our Recursion course:
Part 1: Successive Function Calls
First,
result = calculate_sum(3)is executed, which calls thecalculate_sum()function with 3 as argument. Let's simply call this function call ascalculate_sum(3)Notice the statement
return n + calculate_sum(n - 1);. Insidecalculate_sum(3), this statement becomesreturn 3 + calculate_sum(2).Here, the
returnstatement isn't fully executed. Instead, it callscalculate_sum(2)first.In other words, a separate instance of
calculate_sum()is called (with 2 as an argument). Thus,calculate_sum(2)is separate fromcalculate_sum(3). Please remember that no value has been returned so far.Similarly,
calculate_sum(2)executesreturn 2 + calculate_sum(1). This means a separate functioncalculate_sum(1)is called without returning any value.
Part 2: Successive Value Returns
We left off at when
calculate_sum(1)is called. Now, let's pick off right where we left off.calculate_sum(1)directly returns the value 1 without calling any further functions. That's becausecalculate_sum(1)is the base case where theif (n == 1)statement is executed.This return value of
calculate_sum(1)then goes back tocalculate_sum(2).Remember that
calculate_sum(2)executesreturn 2 + calculate_sum(1). Sincecalculate_sum(1)returned 1, thisreturnstatement becomesreturn 2 + 1, i.e.,return 3.In other words,
calculate_sum(2)returns the value 3 tocalculate_sum(3).Then,
calculate_sum(3)executesreturn 3 + calculate_sum(2), which isreturn 3 + 3, i.e.,return 6.Thus,
calculate_sum(3)returns the value 6 toresult.
And that's how we get 6 as the final result.
Hope that helps! Contact us again if you have further questions.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer
