return value

could someone explain to me what is different in return 0 and return -1

   /* binsearch:  find x in v[0] <= v[1] <= ... <= v[n-1] */
   int binsearch(int x, int v[], int n)
       int low, high, mid;

       low = 0;
       high = n - 1;
       while (low <= high) {
           mid = (low+high)/2;
           if (x < v[mid])
               high = mid + 1;
           else if (x  > v[mid])
               low = mid + 1;
           else    /* found match */
               return mid;
       return -1;   /* no match */ ----------------->?

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:

>does return -1 means return v[-1]?
no. the function is written to return the index in the array v that contains the value v.
return -1 means that the value is not found, and to indicate that the "index" -1 is returned.

>does it must use -1 or can be any negative number?
it could be any negative number, but the code that calls the function has to know about that special return value to compare to, unless it checks if the returned value is in the bounds (0  - n-1) of the array size.

note that -1 is used in most cases when developing functions that return index positions of values to be found.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
return 0 means the value is found on the first position in the array, the array starting at index 0, as explicitely noted in the comment   /* binsearch:  find x in v[0] <= v[1] <= ... <= v[n-1] */

return -1 means the value is not found in the array at all, again as noted in the comment:
/* no match */ ----------------->?
rmtogetherAuthor Commented:

does return -1 means return v[-1]? does it must use -1 or can be any negative number?
All Courses

From novice to tech pro — start learning today.