On 16 January 2012 11:22, Calvin Morrison <mutantturkey@gmail.com> wrote:


On 16 January 2012 09:59, Darrell Anderson <humanreadable@yahoo.com> wrote:
> > When a C++ (boolean) function contains multiple
> if-else tests, and each of those tests contains a return
> statement (return=false; or return=true;), does the function
> exit when encountering the first return?
> >
> > Or does the function continue executing the remaining
> code within that function?
> >
> > In other words, when encountering that first return,
> does the function exit much like a break command?
>
> Code always returns from the function when the first return
> is executed.

Ok. Thanks. I haven't gotten that far in my studies of C++ and none of the simplistic examples on the web provided an answer. :)

Darrell

Consider this function however

int main() {
  // define two integers
  int x = 3;
  int y = 4;

  //print out a message telling which is bigger
  if (x > y) {

  }
  else {
    cout << "x is smaller than y" << endl;
  }
  return 0;
}


Sorry! gmail keeps sending my emails half way done!


int main() {
  // define two integers
  int x = 3;
  int y = 4;

  //print out a message telling which is bigger
  if (x > y) {
  return 1;
  }
  else {
    cout << "x is smaller than y" << endl;
  }
  return 0;
}


In this example, if X > Y then it would return. of course if X was not greater than Y it would return a the end of the function normally.