I'm playing around with recursive functions a bit, and here is what I have for seeing if an array contains a value:
bool val_in_array_r(int val, int array[], size_t size)
{
if (!size)
return false;
else
return (val == *array)|| val_in_array_r(val, array+1, size-1);
}
One question I have here, is I could have an array of a billion entries, and the 10,000th one returns true
. Instead of having to continue and getting:
false
||false
|| ... ||true
||false
||false
|| ...
Is there a way to exit early if the case is met? Or this is just not something a recursive function can do?
It seems to me (theoretically) doing something like that would need to be super low-level, as it'd need to keep track of the initial call frame of the first time val_in_array_r
is called, and then jump back to that rbp
, then replace rsp
and do ret
.