rose by any other name would smell as sweet |
That is to say that an array is not a single number. It must have both starting address and number of elements. The function has to receive the data somehow.
One could play trickery and hide some/all info in global variables, but that is a poor, restrictive workaround that merely reduces the number formal function parameters.
The "best" way might be the use of standard library containers, like
std::vector
. One argument encapsulates all data about the array. The standard library algorithms, however, do not take one container parameter; they expect a pair of "iterators" to the container.
No matter what you do, you do need more than two arguments.
Your current recursion is very close to a iterative loop. Such recursion does not gain you anything.
More importantly, it does not make use of a crucial property of the input data:
What if the searched value is in the last element (or not at all in the array)? Your algorithm has to compare every element in the array in order to find that out.
If the input is already sorted, then the recursion can implement a much smarter search strategy.
PS. IMHO the function should return a value and not print anything. Let the caller (main) can use the value as it likes. Of course, the function should then be able to return an index that means that there is no element with the searched value ...