|
|
In function 'int search(int*, int, int)': 17:1: warning: control reaches end of non-void function [-Wreturn-type] |
but IF line 6 is false and line 10 is true, then you don't return anything |
Write a recursive function that takes a sorted array and a target element and finds that element in the array (returning the index, or -1 if the element isn’t in the array) |
|
|
else return find( array, array_size, pos+1, value_to_find ) ; |
else find( array, array_size, pos+1, value_to_find ) ;
Anything at all can happen; the Standard imposes no requirements. The program may fail to compile, or it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended. - C FAQ |
Without the return, what you are seeing is 'undefined behaviour' |
|
|
> clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp && ./a.out 0. array_size: 5 pos: 0 value_to_find: 0 => 0 found at position 0 - return 0 *** result is 0 *** ------------------ 0. array_size: 5 pos: 0 value_to_find: 2 => 2 not found at position 0 - return find( ... 1 ... ) 1. array_size: 5 pos: 1 value_to_find: 2 => 2 not found at position 1 - return find( ... 2 ... ) 2. array_size: 5 pos: 2 value_to_find: 2 => 2 found at position 2 - return 2 1. find returned 2 - return 2 0. find returned 2 - return 2 *** result is 2 *** ------------------ 0. array_size: 5 pos: 0 value_to_find: 4 => 4 not found at position 0 - return find( ... 1 ... ) 1. array_size: 5 pos: 1 value_to_find: 4 => 4 not found at position 1 - return find( ... 2 ... ) 2. array_size: 5 pos: 2 value_to_find: 4 => 4 not found at position 2 - return find( ... 3 ... ) 3. array_size: 5 pos: 3 value_to_find: 4 => 4 not found at position 3 - return find( ... 4 ... ) 4. array_size: 5 pos: 4 value_to_find: 4 => 4 found at position 4 - return 4 3. find returned 4 - return 4 2. find returned 4 - return 4 1. find returned 4 - return 4 0. find returned 4 - return 4 *** result is 4 *** ------------------ 0. array_size: 5 pos: 0 value_to_find: 6 => 6 not found at position 0 - return find( ... 1 ... ) 1. array_size: 5 pos: 1 value_to_find: 6 => 6 not found at position 1 - return find( ... 2 ... ) 2. array_size: 5 pos: 2 value_to_find: 6 => 6 not found at position 2 - return find( ... 3 ... ) 3. array_size: 5 pos: 3 value_to_find: 6 => 6 not found at position 3 - return find( ... 4 ... ) 4. array_size: 5 pos: 4 value_to_find: 6 => 6 not found at position 4 - return find( ... 5 ... ) 5. array_size: 5 pos: 5 value_to_find: 6 => 6 not found in positons 0 to 4 - return -1 4. find returned -1 - return -1 3. find returned -1 - return -1 2. find returned -1 - return -1 1. find returned -1 - return -1 0. find returned -1 - return -1 *** result is -1 *** ------------------ |