Return values in while loops in functions.

  • #1

Homework Statement

I'm just trying to interpert how the return statement works.

bool check(int item, vector<int>& v2)
{ int i = 0;
while (i < v2.size())
{ if (item == v2)
return true;
i++;
}return false;
}

Homework Equations





The Attempt at a Solution



I'm confused about the return parts of this problem. I does the return false even get excecuted? I thought that once a return statement is reached in a function, the function exits. Or do I have to wait until the last "true" return for the function to exit?
 

Answers and Replies

  • #2
673
2
Think about the control flow of the code. The return true follows an if statement, so what does that mean? If that if conditional doesn't evaluate to true, what happens?
 

Related Threads on Return values in while loops in functions.

  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
2
Views
690
  • Last Post
Replies
1
Views
2K
Replies
0
Views
727
Replies
3
Views
1K
  • Last Post
Replies
3
Views
2K
Replies
4
Views
908
Replies
5
Views
4K
  • Last Post
Replies
7
Views
1K
Replies
10
Views
2K
Top