I have a function whose 'argument' and 'return type' will always be a unsigned (positive) integer.
For example, if we want to find index of fabonnaci series, the index(return type) will always be unsigned(indexs of an array are always positive). Also all elements in a fabonnaci series are unsigned(Fibonacci starts from 0 and goes onward to 0,1,1,2,3,5.. ) so the input argument will always be unsigned.
So i keep the return type 'unsigned int' like in the below code.
1 2 3 4 5 6
|
unsigned int findIndex(unsigned int fabonnaci_value)
{
}
|
In case this function does not successfully find the index for input(e.g if we search the index for value '4' which is not a part of fabonnaci, we won't have a valid index to return) what should i return. Normally I see
in case of failed function. But just for this one situation, i think it is not efficient to change the return type of function to 'int' from 'unsigned int' as the range of output values is decreased drastically.
What should i do here as an alternative? I want to know how these situations are handled/justified by professionals?