int test(vector<int> input, int ind){
if (input[ind]!=5) {
ind++;
test(input, ind);
}
else {
cout<<"---test2 ind="<<ind<<endl;
return ind;
}
}
int main(int argc, constchar * argv[]) {
vector<int> input=vector<int> {1,2,3,4,5};
int ind=0;
cout<<"result="<<test(input, ind)<<endl;
}
The output is:
---test2 ind=4
result=0
However, I expect 'result=4'.
You see that the 'cout' value is Different as 'return' value, although print is just followed by return.
Could anyone explain where is wrong?
In function 'int test(std::vector<int>, int)': 15:1: warning: control reaches end of non-void function [-Wreturn-type]
You return ind in the case you found the value (line 8), after that it is dissmissed. You need to return ind; in any case (move line 8 after line 9). On line 4 you need to assign the result to ind.