If you were to search a sequence for an element, and return the index of that element, you'd want some way to show that the element wasn't found. A negative value is an invalid index in many languages, so returning -1
could only mean the element couldn't be found for one reason or another.
int index_of(const int *array, int sz, int elem) {
for (int i = 0; i < sz; ++i) {
if (array[i] == elem) { // found the element
return i; // return the index of the element
}
}
return -1; // couldn't find it
}
Then in the calling code you might have
int calling() {
int array[N];
// populate array with data ...
int idx = index_of(array, N, 4); // find where 4 is in the array
if (idx < 0) {
// 4 isn't in the array
} else {
// 4 IS in the array !
}
}