Brief description of recursive functions

The characteristic of the recursive function is that the function calls itself internally
. Let me explain with an example:

def add_odd(num)
	if num == 1
		return 1
	return 2 * num - 1 + add_odd(num-1)
a = add_odd(3)
print(a)

The output result is 9 and the
above result is realized by 5+3+1.
When the function is called for the first time, the return is 5+add_odd(2).
At this time, add_odd(2) is called internally by the function called for the first time and returns 3+add_odd(1)
At this time, add_odd(1) directly returns 1 in the if statement, and
finally transfers these data levels upwards, to get 5+3+1=9

Guess you like

Origin blog.csdn.net/weixin_48445640/article/details/108811238