Help with Function Return example | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with Function Return example

I try to understan by myself but I just could not do it. I need help with the process of this example <?PHP function math($t) { if($t==0) return 0; return $t + math($t - 1); } echo math(6); ?> The result is 21 Why?

21st Apr 2019, 4:41 PM
Jorge Cueva
Jorge Cueva - avatar
1 Answer
+ 2
In this example is used recursion => a function which calls itself. It's defined the base case ($t==0 ). The function will call itself until the base case is reached. So the result is 6+5+4+3+2+1 => 21.
21st Apr 2019, 5:28 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar