+ 2

Recursion Help C

Does the increment of variable n changes her value in the next step? Are we calling the function for(brojevi[0]) or brojevi[1]? We have solved this in school without changing the value and I am confused how is it working? Int test3(int brojevi, int n, int s) { If(n==4) return s; else return test3(brojevi, ++n, s*=(brojevi+n)); } int main() { int a[ ]={1,3,4,5}; int n=0; printf("%d", test3(a,n,1)); }

4th Feb 2022, 11:07 PM
dreamer
dreamer - avatar
1 Answer
+ 5
The behaviour changes depending on the compiler, or even between versions of the same compiler and because of that code like this should be avoided. Compiler a would use brojevi[0] while compiler b would use brojevi[1]. Your code could work correctly 1 day and break the next just because you upgraded your compiler.
4th Feb 2022, 11:53 PM
Dennis
Dennis - avatar