I'm enrolled in a CS class and we are learning about recursion. The teacher showed us some code and I'm wondering why it works. buscar is a recursive search function in an array v[]. It returns the position of x in v[]
int buscar(int x, int v[], int n) {
if(n == 0) return -1;
if(x == v[n-1]) return n-1;
buscar(x, v, n-1);
}
int main() {
int v[] = {5, 3, 9, 12};
printf("%d\n", buscar(5, v, 4));
}
The problem here is
buscar(x, v, n-1);
Shouldn't it return 'nothing' and terminate with some error? When I run this program, the result is correct as expected. Why does it work the same as
return buscar(x, v, n-1);