Originally Posted by c_code
The only error message the debugger gives me is :
" 1>c:\users\mo\documents\visual studio 2010\projects\prac5task1.c\prac5task1.c\prac5task1.c(92): warning C4715: 'findNumber' : not all control paths return a value"
Im not too sure what that means...
It means exactly what it says, that there is at least one control path that does not return a value. The function is declared to return a size_t, so it must return a value. Consider your else:
else /* Performs a linear search on the unsorted array */
for (index = 0;index < size; ++index)
if (a[index] == searchKey)
What value is returned if the search fails? More importantly, in the unsorted case, where do you return that failure value?
You don't. That is a control path that does not return a value, which is what the warning says.
You could explicitly return -1 after the for-loop, since the only way you would get there would be if a match was not found. While multiple return's in a function is common practice in C, the rules of structured programming that clifford referred to require that a function have only one exit point, hence only one return. While structured programming isn't The Word of God, following its precepts (eg, no goto's) does promote good practices and helps to keep you from shooting yourself in the foot. When you find a match, save the index and stop the search (eg, by using a break
statement), then return that saved index at the end of the function.
To illustrate the point of using structured programming, here's a problem in your code caused by improper use of multiple returns:
if (sort==1) /*Performs a binary search on the sorted array */
while ( (low <= high) && (found == 0) )
middle = (low + high) / 2;
if (searchKey == a[middle] )
index = middle;
found = 1;
Because you return, the following statement will never be executed. If your compiler were strict enough, it should have warned you about "unreachable code". While this does no actual harm, it makes the variable, found, extraneous; found serves no purpose. Now, if you just save the index of the match and then return it at the bottom of the function (meaning that you have removed and replaced all the other returns), then found would serve the very useful purpose of breaking you out of the search loop because of a match.
This would be a good time for you to stop and read your function for content (just as you would read a message here for the information it contains). Think about what it does. Your use of index is different in the if and the else blocks. In the else, you use it to control the for loop, thus losing the default -1 value, but in the if block you use other variables to index into the array saving the matching index to index before returning it (why not just return middle?). Decide upon a consistent approach and apply it.
And why a size_t? An array index would not be a size_t, but rather an int, so why not an int? Yes, I'm sure that size_t typedef's to int or maybe to unsigned int, but there's a reason for that typedef that I don't see applying here.