0
i need some help with this recursive function
i fail to understand , is the last return statement skipped or what it seems a bit challenging, here is the code https://code.sololearn.com/cTt4T8wuPCFR/?ref=app i need some explanation on this code
6 Réponses
+ 3
Anesensi Jonas ,
to help it is mandatory to get a brief task description what your code should achieve. otherwise we can only guess and maybe waste some time.
> please also describe what your issue is.
+ 1
You function fun takes one parameter n.
If you call the function n will be replaced with the given number. In this case 105.
The function starts with an if statement.
So we have 105 > 100. Which is true.
Then the code inside the if statement will be executed. So it returns 105 - 5. That's why the answer is 100.
The return statement exit the function so that the last return statement will not be reached and executed.
The last return statement will only be executed when the if statement fails. This happens when n is <= 100.
If that is the case it calls itself(recursive).
+ 1
Lothar
sorry if my explanations was insufficient i think i have explained enough
0
Stefanoo ,
i thought that there supposed to be an else statement so that to skip the last statement , due to the procedural nature of how code got executed meaning top to bottom . so i just concluded that since the last return statement didn't had conditions to limit it it was also supposed to be executed.
0
Remember that it will hit the first return statement when it runs, then return the specified variable and control flow returns to outside the function.
0
ok