Python Recursion Function (How to calculate it with two or more arguments) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python Recursion Function (How to calculate it with two or more arguments)

Final amount is 8, but how to get there? Would U guys be so kind to explain it? :) Here is the code : def power(x, y): if y == 0: return 1 else: return x * power(x, y-1) print(power(2, 3))

4th Feb 2019, 9:02 PM
János Nyúl
János Nyúl - avatar
2 Answers
+ 2
1) x = 2 and y = 3 y != 0 so power(2, y-1) 2) x = 2 and y = 2 y != 0 so power(2, y-1) 3) x = 2 and y = 1 y != 0 so power(2, y-1) 4) x = 2 and y = 0 y == 0 so return 1 In 3) return 2 * 1 #2 In 2) return 2 * 2 #4 In 1) return 4 * 2 #8
4th Feb 2019, 10:34 PM
Théophile
Théophile - avatar
+ 1
Thanks :)
5th Feb 2019, 7:08 AM
János Nyúl
János Nyúl - avatar