Return statements

Can I call a function in a return statement? If so, does it return the value of the function called or the value of the current function?
Provided both functions are nonvoid, yes

The value returned is the same value the other function returned.

IE:

1
2
3
4
5
6
7
8
9
10
11
int foo();

int bar()
{
  // this
  return foo();

  // is the same as this:
  int temp = foo();
  return temp;
}
That is awesome, exactly what I wanted to hear. Okay, what about a bool? i.e:

1
2
3
4
5
6
7
8
9
bool foo()
{
    return bar();
}

bool bar()
{
    return true;
}

This should return true, correct? And if foo() and bar() both return false, would that repeat infinite?
yeah, foo() will return true since bar() returns true.

I don't see where the infinite comes from?
and bar() won't return false because you have it returning true.
clarify plz :D
Okay, what about a bool?


It doesn't matter what the return type is as long as it's not void.

return returns whatever follows it, and a function call is "replaced" with the return value. So if you return a function call, you're returning what that function returned.

This should return true, correct?


Correct. foo() would return true, because it returns what bar() returned, which is true.

And if foo() and bar() both return false, would that repeat infinite?


Huh? Infinite? What do you mean?

If bar() in the above returned false, then foo() would return false.
okay, what will be my final return value here if health is below zero and less than full health:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool isDead(int health)
{
    if(health < 0)
       return true;
    return false;
}

bool isFullHealth(int health)
{
    if(health < 100)
        return isDead();
    return true;
}

I keep wanting to think that each function is calling each other and returning false. I'm still in the process of eliminating other semantic errors, thanks for the help so far.
if health is < 100, then isFullHealth returns whatever isDead returns.

isDead returns true if health is below 0, false otherwise.

So:

if health is below zero:

1) isFullHealth will call isDead
2) isDead returns 'true'
3) isFullHealth returns what isDead returned: 'true'


isFullHealth doesn't seem to make a lot of sense. I don't see why it would need to check isDead at all.
I read somewhere that in game programming, you should always call isDead in every function. If pHealth is really less than 0, and somehow program control ends up in isFullHealth (I call this function often too), it needs a way to exit; I do have a bit more going on in my actual isDead and isFullHealth functions. Then again, I am completely new to programming and this is my first week; maybe I am trying to accomplish too much without implementing classes, which I don't know too much about (code wise) yet.
I read somewhere that in game programming, you should always call isDead in every function


You might be misinterpretting or taking it out of context.

You would want to check if the object is dead before assigning additional damage, or before enacting AI or something like that. But for polling the state of their health, there's no point.

Ask yourself this question:

Is the object's alive/dead state change whether or not they have full health?

If the answer is yes, then yeah, call isDead. If the answer is no, then don't.


Always be weary of "always do this" rules. People like to carve out rules like that, but in reality there are very very few of them.

Just use your head. Write code that makes sense.


EDIT:

Maybe what you meant is like... if determining whether or not the object has full health is a lengthy process...

1
2
3
4
5
6
7
bool isFullHealth()
{
  if(isDead())     // if we're dead
    return false;  // then we obviously don't have full health!

  // otherwise, do a much of crap here to determine whether or not we have full health
}
Last edited on
I think I was taking it out of context...and I just learned how to use void functions and passing by reference; making this even more fun now that I can get more than one value out of a function. Taking your advice, my functions are working and I am getting the correct health value, thank you. Why would my (theoretically) object need to know if another object is at full health? I was thinking along the lines of just checking isDead on the object, if false, my object can kill it.
Topic archived. No new replies allowed.