B1 · Episode 19 · 4:41
Tracing Python Function Execution by Hand
This episode teaches how to manually trace the execution of Python functions step by step—covering function definition, parameter passing by position, variable updates during execution, and return value handling—using paper-based simulation before verifying with Python.
Source:jmbarbier / peertube.solidev.net · License:CC BY-SA 4.0
Transcript
Alors sur cette partie, on va revenir un petit peu sur les fonctions.
So, in this section, we’ll briefly review functions.
Je vous ai dit que c'était un des points les plus importants et peut- être les plus
I’ve told you before that this is one of the most important topics—
compliqués aussi du programme.
and perhaps also one of the most challenging parts.
Donc on va prendre le temps pour être sûr que tout le monde sait lire,
So we’ll take the time to make sure everyone can read—
exécuter des fonctions.
and execute functions.
Et entre autres, pour pouvoir écrire une fonction,
Moreover, to write a function yourself,
il faut comprendre vraiment comment ça fonctionne.
you must truly understand how it works.
Et pour ça, il faut être capable de
To do this, you need to be able to—
savoir ce que fait une fonction, prévoir ce que va faire une fonction.
know what a function does, and predict what it will do.
En prenant le temps qu'il faut pour écrire les différentes opérations,
By carefully writing down each operation—
qui est appelé avec quelle variable.
including which variable it’s called with.
Un peu comme qu'on avait fait en assembleur,
It’s a bit like what we did earlier with assembly language—
vous allez retrouver un petit peu des notations et des
you’ll encounter some notations and techniques from assembly language again—
manières de faire de l'assembleur.
though adapted slightly.
On va prendre en fait quelques fonctions et on va les faire à la main,
In fact, we’ll pick a few functions and trace them step by step manually—
on va les exécuter sur le papier et puis ensuite après on prendra le Python et puis
that is, execute them on paper; then later, we’ll use Python and supporting tools—
des outils pour pouvoir aller vérifier que ce qu'on a fait est correct.
to check whether our manual work is correct.
Donc voilà un premier exercice où on a une fonction et on va voir comment un petit
Here’s a first exercise: we’re given a function—
peu dérouler les différentes opérations pour ça.
we’ll walk through its operations step by step.
Donc quand Python va lire le programme,
When Python reads the program,
il va partir du début du programme ici et il va arriver à une définition de
it starts from the beginning and reaches this function definition—
fonction. Quand il arrive à une définition de fonction, il continue,
when it hits a function definition, it doesn’t run it yet—it just keeps reading—
il va jusqu'à la fin, d'accord?
all the way to the end, okay?
Il lit, il apprend simplement la définition de la fonction.
It simply reads and memorizes the function’s definition.
Puis je vais arriver ici, à cet endroit- là.
Then, execution will reach this point.
Là, la première partie qui va être exécutée.
Here, the first part to be executed.
Il va voir le plus intérieur.
It will examine the innermost part first.
Quand on a des parenthèses comme ça, le Python va commencer par exécuter,
When we have parentheses like this, Python starts by executing
par faire ce qu'il y a à l'intérieur.
what’s inside them.
Donc là, en l'occurrence, l'intérieur, c'est cette fonction,
So here, specifically, the ‘inside’ is this function—
fonction 2 3 1, cet appel de fonction.
the call to function 231.
Donc là, le Python va aller sauter
So Python will jump
à l'intérieur de la fonction.
into the function’s body.
Donc là, on va sauter pour aller dans la fonction.
That means we’ll now jump in to run this function.
Et on peut le noter.
We can note this down.
Alors, quand on le note comme ça, c'est un peu comme quand on est en Python,
Marking it this way is a bit like when we’re coding in Python—
ça permet de suivre où
it helps us track where
est- ce qu'on en était.
we currently are.
Mais on va détailler ça, en fait, avec ce qu'on appelle la pile d'appel.
But we’ll actually detail this using what’s called the call stack.
Donc on part. de notre programme principal,
We start from our main program,
et à chaque fois qu'on va appeler une fonction,
and each time we call a function,
on va noter un petit peu plus d'informations sur
we record a bit more information:
avec quoi est- ce qu'elle a été appelée, qu'est- ce qu'elle fait,
such as what arguments it was called with and what it’s doing,
et qu'est- ce qu'elle renvoie.
and what it returns.
Donc là, quand je suis passé de
So, when I move from here
fonction 2 3 1.
to function 231,
là ici qui est l'appel de la fonction,
that is, this location—the function call—
que j'ai sauté maintenant dans l'exécution de la fonction qui est là,
and I’ve now jumped into its execution,
je vais le noter en utilisant une petite flèche comme ça qui dit ok j'étais dans le
I’ll mark it with a small arrow like this, indicating: OK, I was in the
programme principal et j'appelle la fonction qui s'appelle fonction.
main program and am now calling the function named ‘function’.
2. Et juste à côté ou juste en dessous,
2. Right next to it, or just below it,
je vais noter les variables qui sont dans cette fonction.
I’ll write down the variables used in this function.
Donc là en l'occurrence, on a.
So in this case, we have:
Un paramètre qui est le paramètre a en première position,
a parameter named 'a' in the first position,
un paramètre qui est le paramètre b en deuxième position,
and a parameter named 'b' in the second position.
et quand je vais le fournir, j'ai ici un appel de fonction où les paramètres sont
When I call this function, the arguments are passed by position:
donnés par position.
that is, in order.
Le 3 qui est là va correspondre au a, le 1 qui est là va correspondre au b.
The 3 here corresponds to 'a', and the 1 here corresponds to 'b'.
Donc mon appel de fonction se fait ici avec la variable paramètre a,
So in this function call, the parameter 'a' is:
donc qui fait une variable a
a variable 'a' whose value is 3,
qui vaut 3. Et le b qui vaut 1.
and 'b' has the value 1.
D'accord, donc la variable a contient 3, la variable b contient 3,
Okay, so variable 'a' holds 3, and variable 'b' holds 1,
puisqu'on a passé le paramètre première position a, deuxième position b.
since we passed 'a' as the first argument and 'b' as the second.
Ensuite, je vais faire l'étape d'après: b est égal à a moins b.
Next, we perform the next step: b equals a minus b.
Donc b est égal à moins b, ça me fait b est égal à a moins b.
That is, b equals a minus b — so b becomes a minus b.
C'est égal à 3 moins 1,
That equals 3 minus 1,
c'est égal à 2.
which equals 2.
Donc b contient 2 maintenant et a n'a pas changé.
So now 'b' holds 2, while 'a' remains unchanged.
Et enfin, je vais renvoyer b.
Finally, I return the value of 'b'.
Donc je renvoie la valeur 2.
That is, I return the value 2.
Donc là ici, je fais une flèche qui revient vers le programme principal avec la
So I draw an arrow returning here to the main program,
valeur de retour que je vais noter dessus.
and label the return value on it.
Il renvoie la valeur 2.
It returns the value 2.
Et donc mon return B fait que je reviens
So the 'return b' statement brings me back to
à la position.
the position where the function was called.
De j'étais parti.
I had already left.
Maintenant, mon fonction qui est là,
Now, the function I called earlier,
je suis revenu de l'appel de la fonction,
I’ve returned from calling it;
c'est comme si le fonction n'existait plus et qu'on avait remplacé ça par la valeur
it’s as if that function no longer exists,
qui arrivait de là.
replaced instead by the value it returned.
ici, la valeur qui correspond ici maintenant c'est 2.
Here, the value now present is 2.
Donc ce que je vais avoir comme affichage, c'est print 2.
So the output we’ll see is: print 2.
Donc si je dessine comme on le faisait un écran quelque part,
If we draw a screen somewhere, like before,
voilà mon écran.
here’s my screen.
J'ai ici un affichage qui fera 2 à cet endroit.
At this spot on the screen, it will display the number 2.