2

Please explain how the return call from nested recursive function propagates all the way out to main?

I came across the following code segment in a book. The printf output is '6, 12'

In the recursive function sumdig(), a return statement is called in third recursive call.

When sumdig() now returns to the second recursive call, sumdig() should evaluate to an expression, not a return statement (i.e. return sumdig(n)).

But that is not what seems to happen. The return call from the recursive function is being propagated all the way out to main.

Can anyone please explain how the return call from nested recursive function propagates all the way out to main?

The code below would have made sense to me if the recursive call was something like 'return sumdig(n)'.

main()
{

  int a, b;

  a = sumdig( 123 );
  b = sumdig( 123 );

  printf( "%d, %d\n", a, b);

}

sumdig(int n)
{
  static int s = 0;
  int d;

  if(n != 0)
  {
    d = n % 10;
    n = (n - d) / 10;
    s = s + d;

    sumdig(n);

  }
  else
    return(s);
}

Recursive calls for Sumdig

Initial call           :n = 123, s = 0
First recursive call   :n = 12,  s = 3
Second recursive call  :n = 1,   s = 5
Third recursive call   :n = 0,   s = 6   // Return statement happens here

Likewise For Second call...the static variable will increment by 6 again to become 12.

If my question is not clear, please help me improve it.

4
  • what does sumdig return ? an int ? Commented Aug 22, 2013 at 11:53
  • which complier are you using? Whether any warning during compilation? I am getting output like this 4199344, 4199344 Commented Aug 22, 2013 at 11:55
  • Yes, i lifted the code from a book. Sumdig is declared in Old style K&R format. So return is an implicit int. Commented Aug 22, 2013 at 11:56
  • Using gcc on linux 64-bit. No warnings. Commented Aug 22, 2013 at 11:57

3 Answers 3

4

The return value is undefined, because only the innermost recursive call returns with a defined value s. All previous calls for sumdig() are returning with an undefined value, because there is no return statement when the sumdig returns without fullfilling the if` clause i.e when it returns from a recursive call.

So it might be, that s is propagated back, if the return value in the register stays unchanged, which may, or may not be the case.

To be safe, the recursive call should be:

return sumdig(n);
Sign up to request clarification or add additional context in comments.

Comments

1

This is very old style C. The return value from a function will be the result of the last evaluation in the function (in this case sumdig()).

8 Comments

noz, I was thinking along those lines. So i put a the statement 'd = 99;' at the end of sumdig() function. But output is still '6, 12'?
@kutichatan : try to run the program with the help of a debugger , you 'll get the complete trace .
Odd - perhaps the d=99 has been optimized out?
Just ran through the debugger, with dissassembly view in eclipse. The d=99 statement seems to be executed.
Even in old style C, the return value of a function was not the result of the last expression. If the end of a function is reached without encountering a return statement and you then try to use the return value of that function, the behavior is and always has been undefined. What is old style though is being allowed to not give return types to the functions.
|
1

Your question itself has the answer. static variable s updated each recursion when if condition false that time only function return to main. So last updated value of s return to main.

1 Comment

Not quite because s is only returned from the bottom level call (when n is 0)

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.