i need some help with this recursive function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

5th Oct 2023, 3:33 PM
Anesensi Jonas
Anesensi Jonas - avatar
6 Answers
+ 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.
5th Oct 2023, 7:10 PM
Lothar
Lothar - avatar
+ 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).
5th Oct 2023, 7:20 PM
Stefanoo
Stefanoo - avatar
+ 1
Lothar sorry if my explanations was insufficient i think i have explained enough
6th Oct 2023, 3:50 AM
Anesensi Jonas
Anesensi Jonas - avatar
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.
6th Oct 2023, 3:49 AM
Anesensi Jonas
Anesensi Jonas - avatar
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.
7th Oct 2023, 3:46 AM
Jonathan Shiell
0
ok
7th Oct 2023, 4:51 AM
Anesensi Jonas
Anesensi Jonas - avatar