I just have a quick question. while implementing linear search in pset3, why is it that when I use "return 0" instead of "return true", the search function acts weird. Like it'd sometimes only find the needle if it's in the middle of the hay or at the beginning? I'm just curious. Shouldn't "return 0" and "return true" indicate the same thing?

1 Answer 1


The problem with that is that true is 1, not 0, so return true is the same as return 1. If the return type really is bool, then you're returning the exact opposite of what you want to return.

You may be confused by this and the standard retrun values used by main and other standard functions. In many cases, they return an int, but use 0 to indicate success and non-zero values to indicate either a failure in general, or specific values to indicate specific conditions that occurred.

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

  • So just to be clear, you're saying that when I'm using an outside function and I want to send it's output to another piece of code somewhere else, I should use 1 to indicate success, while functions within the same code should return 0 to indicate success or "no errors". Is that correct?
    – Ahmed
    Aug 18, 2015 at 6:31
  • No. The standard is this: when any function is using the return value to indicate success or failure, 0 is used for success and a non-zero to indicate a failure. The actual non-zero value is whatever you define it to be to indicate whatever conditions you want (or it is already defined by whoever wrote it). Generally, this will be an int value. IF you are not using the return value to indicate success or failure of the function's execution, then it is either not returning anything (return type is void), or is some other data type, and a value of that data type must be returned.
    – Cliff B
    Aug 18, 2015 at 7:15
  • Understand that there's a difference between the success/failure of the function to execute as just described, vs. returning a test result. In the question, the function returns true or false, a boolean, based on whether the number is found or not. To illustrate what I'm trying to say, the function may return a true or a false, but either way, it executed successfully. Since the function is very, very likely to succeed, returning an indicator that it executed properly serves little purpose. However, the whole point of the function is to actually say if it found or didn't find the target.
    – Cliff B
    Aug 18, 2015 at 7:20
  • Great! much more clear now. Thanks a lot :)
    – Ahmed
    Aug 19, 2015 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .