I
I
Ivanzolo20012021-08-19 21:16:18
recursion
Ivanzolo2001, 2021-08-19 21:16:18

Question for python programmers?

I have a function, there is nothing blocking there, and at the end of the functions, the same function is called again. After it calls itself and the second function starts running, will the first one finish?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mikhail Krostelev, 2021-08-19
@twistfire92

this is called recursion. Read.
If you watched the movie "Inception", then it's like a dream within a dream from this movie. The first function call is the first sleep level, the second call is the second sleep level, and so on. And as in the same movie there is a "ejection", the same is the exit from the entire stack of functions. Well, specifically, if you take your example.
And the question generally does not apply to python. The same is true for any other languages.

R
Roman Kitaev, 2021-08-19
@deliro

Not

L
low molecular macro, 2021-08-19
@molekulyarniy

the first function will not complete, and execution will jump to the second function. Upon completion of the second function, the first will continue its execution. This is in your case which you gave as an example

A
Alexandroppolus, 2021-08-25
@Alexandroppolus

The case where a recursive call is the very last action of a function is "tail recursion". In some languages ​​and runtimes, there is a "tail recursion optimization", that is, the compiler rewrites this so that the current call completes before the next one, and thus there will be no stack overflow, no matter how deep the recursion is.
In programming languages ​​where the cycle was not delivered, this makes sense. In normal programming languages, such a feature is useless, because it is easy to convert it to a loop manually. It is not clear whether it exists in Python, a cursory search did not give an answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question