S
PRO
yesterday
Stephencountry asked

Can you guys write this math problem out better for me since I can't see this one?

Udayan Shakya
Expert
10 hours ago
Udayan Shakya answered

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

  1. First, result = calculate_sum(3) is executed, which calls the calculate_sum() function with 3 as argument. Let's simply call this function call as calculate_sum(3)

  2. Notice the statement return n + calculate_sum(n - 1);. Inside calculate_sum(3), this statement becomes return 3 + calculate_sum(2).

  3. Here, the return statement isn't fully executed. Instead, it calls calculate_sum(2) first.

  4. In other words, a separate instance of calculate_sum() is called (with 2 as an argument). Thus, calculate_sum(2) is separate from calculate_sum(3). Please remember that no value has been returned so far.

  5. Similarly, calculate_sum(2) executes return 2 + calculate_sum(1). This means a separate function calculate_sum(1) is called without returning any value.

Part 2: Successive Value Returns

  1. We left off at when calculate_sum(1) is called. Now, let's pick off right where we left off.

  2. calculate_sum(1) directly returns the value 1 without calling any further functions. That's because calculate_sum(1) is the base case where the if (n == 1) statement is executed.

  3. This return value of calculate_sum(1) then goes back to calculate_sum(2).

  4. Remember that calculate_sum(2) executes return 2 + calculate_sum(1). Since calculate_sum(1) returned 1, this return statement becomes return 2 + 1, i.e., return 3.

  5. In other words, calculate_sum(2) returns the value 3 to calculate_sum(3).

  6. Then, calculate_sum(3) executes return 3 + calculate_sum(2), which is return 3 + 3, i.e., return 6.

  7. Thus, calculate_sum(3) returns the value 6 to result.

And that's how we get 6 as the final result.

Hope that helps! Contact us again if you have further questions.

C
This question was asked as part of the Learn Recursion With C course.