why we return -1 when the condition is not met?
Linear search return -1
If the element is found in the array we can return the index at which it is found but if it is not present in the array then we have to return a number which cannot be an index of the array. So, we return -1 because index of array can never be negative. It is basically to denote the ‘false’ condition that the element could not be found. We cannot return ‘false’ since return type of the function would be ‘int’. You can return any negative integer, it is just a convention to return -1.
1 Like
Please mark the doubt as resolved if you have no further questions about this 