B1 · Episode 24 · 4:43
Understanding Python Function Call Flow
This episode walks through the step-by-step execution flow of nested function calls in Python, showing how parameters are passed, how return values replace expressions in the calling context, and how the call stack unfolds.
Source:jmbarbier / peertube.solidev.net · License:CC BY-SA 4.0
Transcript
Donc je reviens à mon programme à plan, et cette fois la fonction f,
So I return to my main program, and this time the function f
pareil, n'existe plus, et l'ensemble de ce que j'avais là.
likewise no longer exists, and all the code I had written there
est remplacé par la valeur de retour qui vaut douze.
is replaced by the return value, which is 12.
Puis dernier appel, cette fois je réappelle ma fonction g.
Then comes the final call: this time, I call function g again.
Avec le paramètre 12, donc c'est à nouveau un appel.
with parameter 12—so this is once again a function call.
le paramètre a vaut 12,
Here, the parameter a equals 12,
et je vais renvoyer trois fois cette valeur.
and I’ll return three times this value.
Donc je reviens dans mon programme principal
So I return to the main program,
en renvoyant 36 points.
returning 36.
Donc retour programme principal,
Back in the main program,
et cette fois, toute cette partie- là
this entire section here
est remplacée par la valeur de retour.
is replaced by the return value.
Donc là ici 36, ce qui me fait que à la fin.
So here we have 36, meaning that finally,
J'ai un print 36 et un magnifique écran sur lequel s'affichera juste le nombre
the program runs 'print(36)', and a beautiful screen shows just the number
36. On peut aller vérifier en Python.
36. We can verify this in Python.
36. Et si on fait l'exécution,
36. If we run the code,
on va retrouver le même ordre d'exécution que ce qu'on a fait.
we’ll see the same execution order as we just analyzed.
déclaration appel de g,
First, declare and call function g,
ressort de g. Puis appel de f, on est là,
g returns; then call function f—we’re here now,
puis on ressort, puis on appelle le g.
after f returns, we call g again.
Et on ressort et on fait l'affichage print.
g returns again, and finally we execute the print statement.
si vous voulez garder l'historique des différentes fonctions qui ont été appelées,
If you want to keep a history of all functions that have been called,
quand vous êtes dans Python Tutor, vous pouvez, quand vous écrivez ici le code,
when writing your code in Python Tutor,
lui dire de montre et toutes les show all frames ici,
you can click here to select 'Show All Frames',
ça va garder tous les appels passés, tous les appels passés
so it keeps all past function calls,
des fonctions, ne pas les
that is, every function that has been called,
effacer une fois que la fonction est terminée.
without erasing them once the function finishes.
Donc là, si je reprends celui- là, avec la même manière.
So now, if I reuse this one, using the same method.
Voilà, vous voyez que ici en fait il garde en fait simplement l'historique de tout
Look—you’ll see that here it actually just keeps a history of everything that happened.
ce qui s'est passé, visuellement ça permet de garder,
Visually, this allows us to
de revenir un peu dans le passé en arrière.
go back a bit into the past.
Je le fais pas parce que ça prend plus de place,
I don’t do this because it takes up more space,
mais ça peut être pratique de le faire.
but it can be handy to do so.
Et on revient sur l'exercice suivant.
Now let’s move on to the next exercise.
Alors exercice 5, les choses se compliquent
Exercise 5 is here—and things get even more complicated.
encore. On a des fonctions qui appellent des fonctions dans des fonctions.
We now have functions calling other functions, which in turn call yet more functions.
à vous de jouer, il y en a pour un certain temps et je pense que vous pouvez ne pas
It’s up to you to try solving it—it’ll take some time,
faire éventuellement l'exercice 6 qui est encore peut- être plus compliqué.
and you may even skip Exercise 6 for now, as it’s possibly even more challenging.
On va voir. Si vous avez le temps, faites le 6, mais il est vraiment
We’ll see later. If you have time, try Exercise 6—but it’s truly
costaud. A few moments later.
tough. See you in a moment.
Alors, correction pour l'exercice cinq.
Now, let’s go over the solution to Exercise 5.
On a trois fonctions qui sont définies.
We have three functions defined.
Donc mon programme avance et ne démarre qu'en fait ici.
My program runs step by step—and actually starts executing right here.
Tout ça, c'est de la déclaration de fonction, ça ne fait rien.
All this is just function declarations—nothing runs yet.
Le programme commence par un appel, mon programme principal,
The program begins with a call—this is the main program,
je suis là, il commence par un appel.
and I’m right here—it starts with that call.
avec W de 1. Donc je passe
passing W = 1. So I pass
A W et non pas.
W—not something else.
Et w va faire un print de tout ça.
Then W prints out all of this.
Donc je passe de la fonction principale à la fonction w.
So I jump from the main function to function W.
Et la variable a vaut
Inside W, the variable a equals
un. Ensuite à l'intérieur de w, je vois que j'ai un h
1. Then, inside W, I see a call to h(a),
de a, donc je vais sauter à
so I’ll jump to
l'exécution de h de a.
the execution of h(a).
A ici, c'est un appel avec
Here, this is a call
la fonction h.
to the function h.
Je fournis l'argument a ici qui vaut 1, donc là ma fonction h,
I pass the argument a, whose value is 1, so here my function h
elle récupère un a qui vaut 1.
receives an a equal to 1.
Et elle va elle- même
then itself
faire un appel à la fonction g de a.
call function g with a.
A vaut 1, donc j'ai un appel ici à la fonction g.
a equals 1, so here I call function g,
avec un a qui vaut 1.
passing an a equal to 1.
Cette fonction g, elle
This function g
affiche la valeur de a.
prints the value of a.
Donc là, je peux faire mon écran.
So now, I can run this code.
Et là, ici, j'ai donc un print a et a vaut 1.
Here on screen, it prints 'a', and a equals 1.