Amit
Amit

Reputation: 7818

Confusing C++ situation

This is a really stupid question, but I need a little reassurance/assistance. I have the current "recursive" situation:

void add( int value )
{
  // do something ... //

  if ( condition ) {
    add(myVal);
    acc = 0;
  }
}

My question is whether the variable acc will eventually be set to zero. It should, right? Even if the condition doesn't hold true the second time around (upon the recursion). It'll just finish performing the function and proceed with setting acc to 0 .. I think / hope?

Thanks!

EDIT: I'm not posting complete code because I don't think it's necessary. It's a bit lengthy. It's worth mentioning that the condition is not always true. Therefore, this recursion is not infinite.

EDIT 2: The max recursions is 2. That is, if the condition holds true the first time, it will outcome false the second time (upon recursion), avoiding an infinite loop. To clarify my question: Consider the situation where the condition first outcomes true, so the recursion takes place. After the function finishes (the outcome will be false during the recursion) and exits, it'll exit to the original function, and that will exit as well. Upon exiting, will it continue where it left off, that is, setting acc=0 and then returning? One of the answers below provided a good explanation for this.

Upvotes: 1

Views: 177

Answers (4)

Mooing Duck
Mooing Duck

Reputation: 66912

As others have said, if acc is local, this does nothing, and if acc is not local, this is redundant. Also, there is no guarantee that acc will be set to zero if condition is false on the first call. You probably want this:

void add( int value )
{
  // do something ... //    
  if ( condition ) {
    add(myVal); //if condition is true, recurse.
  } else {
    acc = 0; //else, set acc to zero once and escape all the recursion
  }
}

Upon exiting, will it continue where it left off, that is, setting acc=0 and then returning?

Yes.

Upvotes: -2

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

If condition is false from the beginning, acc will not be set to zero. Otherwise, yes it will.

So, the only thing that will affect whether acc get's set to zero is the value of condition the first time.

Upvotes: 2

templatetypedef
templatetypedef

Reputation: 372704

Assuming that the recursive call to add returns, yes, the value of acc inside of the function that made the recursive call in the first place will indeed be set to zero. You can think of the code as working like this:

if ( condition ) {
  make a call to add(myVal);
  wait for that call to return;
  acc = 0;
}

An important detail, though, is that, assuming that acc is a local variable in the function, it will only be set to zero in the calling function, not the recursive call you made. For example, if your code looks like this:

void add( int value )
{
  int acc = 1;
  cout << acc << endl;

  if ( condition ) {
    add(myVal);
    acc = 0;
  }
}

Then if you do make the recursive call you indicated, the output of the cout statement in the recursive call will still be 1, because the value of acc in this particular recursive call is independent of the value of acc in other recursive calls.

Hope this helps!

Upvotes: 4

Blindy
Blindy

Reputation: 67362

Yes, functions return right after the point they got called, so your acc will get set to 0.

Note that if it's global, it will be set to 0 multiple times over and over as you're returning from recursion, while if it's local, and that's the end of your function, it might not do anything useful (read, it will get optimized out by your compiler).

Upvotes: 0

Related Questions