I have the following pseudocode which a sequential search pseudocode and I am trying to understand what return -1 mean. why would we return -1 could someone please explain.
A[n] <-- K
i <-- 0
while A[i] != K do
i = i + 1
if(i<n)
return i;
else
return -1; //What is this mean ?
Returning -1 is a way of conveying the fact that the code reached the end without returning in the middle. Returning -1 in this case means that the element K does not exist in the array.
Note that returning 0 is not used for this purpose as it could mean the element is present at the 0th index. If your function was something which could return -1 at some point in the middle, some other return value to indicate failure would have been chosen.