2

is my first time here so i dont know exactly how it works, so sorry for the mistakes.

What's the result from this function, when we give as "aktueller Parameter" the number 3?

(The original text:

Welches Ergebnis liefert diese Methode, wenn bei einem Aufruf als aktueller Parameter der Wert 3 übergeben wird?

Im studying in German, so i dont really know the english terms :/ )

public int m(int p)
{
   int result;
   if (p == 0)
   {
    result = 0;
   }
   else
   {
    result = 3*p + m(p-1);
   }
    return result;
   }

I have already tried it and the answer is 18, but when im trying to do it without any program the answer i find is 15:

result = 3 * 3 + 3(3-1);

Can someone please explain me why is 18 and not 15? Im assuming that i am making something wrong.

Thank you in advance.

3
  • You're mistaking m and p. It's a recursive code, evaluates to 3*3 + m(2), the second part becomes 3*2 + m(1), and m(1) = 3, total is 9+6+3 = 18. Commented Feb 26, 2014 at 17:12
  • I think "aktueller Parameter" == "actual parameter" is close enough. Commented Feb 26, 2014 at 17:25
  • thank you both for the explanations. @AntonH and ajb Commented Feb 26, 2014 at 17:30

4 Answers 4

9

Let's break down this recursive call:

With m(3), p isn't 0, so we return 3*3 + m(2);.

3*3 + (m(2))

With m(2), p isn't 0, so we return 3*2 + m(1);.

3*3 + (3*2 + m(1))

With m(1), p isn't 0, so we return 3*1 + m(0);.

3*3 + (3*2 + (3*1 + m(0))

With m(0), p is 0, so we return 0. Then the recursive call stack unwinds.

3*3 + (3*2 + (3*1 + (0)) =
9 + (6 + (3 + 0)) = 
9 + (6 + 3) = 
9 + 9 = 
18
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much!That was really helpful!! :D (I cant vote up or accept the answer)
You gain the privilege of voting up when you reach 15 reputation, and you are always allowed to accept one answer that you feel helped you the most to solve your problem after 15 minutes.
2

When you see m(p-1), that means that you're calling the function m again from inside itself, which is called recursion.

Essentially, the arithmetic it's doing is 3*3+3*2+3*1 = 18.

Comments

0

Due to recursive code.Please c the strack trace

When you pass the p value as 3,the function interact as below :

 result = 3*p + m(p-1);        // 3*3 + m(2)  = 9
                               // 3*2 + m(1)  = 6
                               // 3*1 + m(0)  = 3 
                                             -------
                                               18

Comments

0
                           // Original argument is 3
3*3 + m(3-1)               // Step #1  New argument is 3 - 1
3*2 + m(2-1)               // Step #2  New argument is 2 - 1
3*1 + m(1-1)               // Step #3  New argument is 1 - 1

                           // p == 0   Return 0

3*1 + 0 = 3                // Step #4  Return 3
3*2 + 3 = 6 + 3 = 9        // Step #5  Return 9
3*3 + 9 = 9 + 9 = 18       // Step #6  Return 18, which is the final value

Here is how I like to work on recursive functions, and it a personal preference. Start at the top, and put together the known values, then write the function on the side, in this case we simply add it. Then, on the next line, write the known values again (this time, one of them is one less (2, rather than 3), and write the function with the new arguments to the right of it (we're once again adding), and so on. I work from top to bottom.

I then show the results on the right side and work back up. So, I don't write the results right away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.