recursion - Passing return values over recursive functions in C -


i writing binary search function:

int binsearch(int seq[], int start, int end, int key) {     int len = end - start + 1;     int mid = (start + end) / 2;      if (len <= 0)         return -1;     else if (key == seq[mid])         return mid;     else if (key < seq[mid])         binsearch(seq, start, mid - 1, key);     else         binsearch(seq, mid + 1, end, key); } 

i not aware returned once in innermost call. compiled gcc in laptop, , worked perfectly.

however, when compiled code in cloud computer using clang, threw me warning control reach end of non-void function. then, put variable there pass return value on recursive calls.

why did gcc compile without warning , yet function gave correct output? kind of compiler optimization? thought c not pass return value automatically on recursive calls.

the behaviour of program undefined; must explicitly return value on control paths.

a kind compiler warn of this; gcc if set appropriate warning level.

i believe fix in case write return before binsearch calls.

(the exception int main(...) compiler must introduce implicit return 0; if it's missing).


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -