Python from entry to practice: function recursion of functions

1. Function recursion

Functions can not only be defined nestedly, but also be nestedly called, that is, in the process of calling a function, another function is called inside the function, and the recursive call of a function refers to the process of calling a function and directly or indirectly call the function itself

There are two types of recursion: direct call and indirect call

1.1 Direct call

In the process of calling f1, f1 is called again, which is to directly call the function f1 itself

 

def f1():
    print('from f1')
    f1()
f1()

1.2 Indirect calls

In the process of calling f1, f2 is called again, and in the process of calling f2, f1 is called again, which is the indirect call function f1 itself

 

def f1():
    print('from f1')
    f2()

def f2():
    print('from f2')
    f1()

f1()

 1.3 Recursive exit conditions

If the recursion does not set the exit condition, it will enter an infinite loop, which is meaningless, so we must set the exit condition!

For example: we input a number randomly, and we want to output an integer between the number and 10 (excluding 10)

Guess you like

Origin blog.csdn.net/weixin_43507744/article/details/126578778