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

  1. 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

  2. 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

  3. est remplacé par la valeur de retour qui vaut douze.

    is replaced by the return value, which is 12.

  4. Puis dernier appel, cette fois je réappelle ma fonction g.

    Then comes the final call: this time, I call function g again.

  5. Avec le paramètre 12, donc c'est à nouveau un appel.

    with parameter 12—so this is once again a function call.

  6. le paramètre a vaut 12,

    Here, the parameter a equals 12,

  7. et je vais renvoyer trois fois cette valeur.

    and I’ll return three times this value.

  8. Donc je reviens dans mon programme principal

    So I return to the main program,

  9. en renvoyant 36 points.

    returning 36.

  10. Donc retour programme principal,

    Back in the main program,

  11. et cette fois, toute cette partie- là

    this entire section here

  12. est remplacée par la valeur de retour.

    is replaced by the return value.

  13. Donc là ici 36, ce qui me fait que à la fin.

    So here we have 36, meaning that finally,

  14. 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

  15. 36. On peut aller vérifier en Python.

    36. We can verify this in Python.

  16. 36. Et si on fait l'exécution,

    36. If we run the code,

  17. 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.

  18. déclaration appel de g,

    First, declare and call function g,

  19. ressort de g. Puis appel de f, on est là,

    g returns; then call function f—we’re here now,

  20. puis on ressort, puis on appelle le g.

    after f returns, we call g again.

  21. Et on ressort et on fait l'affichage print.

    g returns again, and finally we execute the print statement.

  22. 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,

  23. quand vous êtes dans Python Tutor, vous pouvez, quand vous écrivez ici le code,

    when writing your code in Python Tutor,

  24. lui dire de montre et toutes les show all frames ici,

    you can click here to select 'Show All Frames',

  25. ça va garder tous les appels passés, tous les appels passés

    so it keeps all past function calls,

  26. des fonctions, ne pas les

    that is, every function that has been called,

  27. effacer une fois que la fonction est terminée.

    without erasing them once the function finishes.

  28. Donc là, si je reprends celui- là, avec la même manière.

    So now, if I reuse this one, using the same method.

  29. 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.

  30. ce qui s'est passé, visuellement ça permet de garder,

    Visually, this allows us to

  31. de revenir un peu dans le passé en arrière.

    go back a bit into the past.

  32. Je le fais pas parce que ça prend plus de place,

    I don’t do this because it takes up more space,

  33. mais ça peut être pratique de le faire.

    but it can be handy to do so.

  34. Et on revient sur l'exercice suivant.

    Now let’s move on to the next exercise.

  35. Alors exercice 5, les choses se compliquent

    Exercise 5 is here—and things get even more complicated.

  36. 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.

  37. à 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,

  38. 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.

  39. 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

  40. costaud. A few moments later.

    tough. See you in a moment.

  41. Alors, correction pour l'exercice cinq.

    Now, let’s go over the solution to Exercise 5.

  42. On a trois fonctions qui sont définies.

    We have three functions defined.

  43. Donc mon programme avance et ne démarre qu'en fait ici.

    My program runs step by step—and actually starts executing right here.

  44. Tout ça, c'est de la déclaration de fonction, ça ne fait rien.

    All this is just function declarations—nothing runs yet.

  45. Le programme commence par un appel, mon programme principal,

    The program begins with a call—this is the main program,

  46. je suis là, il commence par un appel.

    and I’m right here—it starts with that call.

  47. avec W de 1. Donc je passe

    passing W = 1. So I pass

  48. A W et non pas.

    W—not something else.

  49. Et w va faire un print de tout ça.

    Then W prints out all of this.

  50. Donc je passe de la fonction principale à la fonction w.

    So I jump from the main function to function W.

  51. Et la variable a vaut

    Inside W, the variable a equals

  52. 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),

  53. de a, donc je vais sauter à

    so I’ll jump to

  54. l'exécution de h de a.

    the execution of h(a).

  55. A ici, c'est un appel avec

    Here, this is a call

  56. la fonction h.

    to the function h.

  57. 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

  58. elle récupère un a qui vaut 1.

    receives an a equal to 1.

  59. Et elle va elle- même

    then itself

  60. faire un appel à la fonction g de a.

    call function g with a.

  61. A vaut 1, donc j'ai un appel ici à la fonction g.

    a equals 1, so here I call function g,

  62. avec un a qui vaut 1.

    passing an a equal to 1.

  63. Cette fonction g, elle

    This function g

  64. affiche la valeur de a.

    prints the value of a.

  65. Donc là, je peux faire mon écran.

    So now, I can run this code.

  66. Et là, ici, j'ai donc un print a et a vaut 1.

    Here on screen, it prints 'a', and a equals 1.