Return values in while loops in functions.

In summary, the conversation is about understanding the return statement in a function that checks if a given item is present in a vector. The code uses a while loop and an if statement, with a return statement of true if the item is found and false if not. The question is about when the function exits and how the return statements are executed.
  • #1
torquerotates
207
0

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?
 
Physics news on Phys.org
  • #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?
 

1. What is a return value in a while loop in a function?

A return value in a while loop in a function is the value that is returned by the function after it has completed its execution. This value can be assigned to a variable or used in other expressions.

2. Can a while loop in a function have multiple return values?

Yes, a while loop in a function can have multiple return values. These values can be returned using a tuple or a list, and can be unpacked by the calling code.

3. How do return values in while loops in functions help in program execution?

Return values in while loops in functions help in program execution by allowing the function to return a specific value or set of values, which can be used by the calling code to make decisions or perform other tasks.

4. Can the return value in a while loop in a function be modified?

Yes, the return value in a while loop in a function can be modified by using the return statement within the loop. This allows the function to return different values based on different conditions or inputs.

5. Is it necessary to have a return value in a while loop in a function?

No, it is not necessary to have a return value in a while loop in a function. If the function does not have a return statement, it will return the default value of None. However, using return values can make the function more versatile and useful.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Set Theory, Logic, Probability, Statistics
2
Replies
54
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top