B1 · Episode 25 · 4:42

Python Function Calls and Return Values

This episode explains how function calls work in Python, including the flow of execution, how the 'return' statement passes values back to the caller, and how functions without an explicit return implicitly return 'None'.

Source:jmbarbier / peertube.solidev.net · License:CC BY-SA 4.0

Transcript

  1. Puis elle va renvoyer

    Then it will return

  2. le return, on va renvoyer la valeur de a.

    this return value—we’ll return the value of a.

  3. Donc je reviens ici en faisant un retour qui vaut 1.

    So I return here with a return value of 1.

  4. D'où est- ce que je reviens?

    Where am I returning from?

  5. Je reviens à la fonction qui m'avait appelé.

    I return to the function that called me.

  6. Donc là ici, je reviens à cet endroit- là.

    So right now, I return to this spot.

  7. Le g de a qui était appelé est remplacé maintenant par sa valeur de retour et sa

    The previously called g(a) is now replaced by its return value, and

  8. valeur de retour c'est 1.

    its return value is 1.

  9. Donc ça me fait maintenant un print 1.

    So now it executes print(1).

  10. Donc j'ai à nouveau ici un 1 qui va s'afficher.

    So another 1 appears here.

  11. Puis je passe à l'étape d'après,

    Then we move to the next step,

  12. return deux fois a.

    returning twice a.

  13. Donc cette fonction, là je suis toujours dans la fonction h.

    Right now, I’m still inside function h.

  14. Ma fonction h va, elle, donc ici,

    My function h will, right here,

  15. mon h, on dirait un b.

    my h looks like a b.

  16. Ma fonction h qui est là va elle- même revenir

    This function h itself will then return,

  17. en renvoyant deux fois a, on voit ici que le a valait 1 maintenant,

    returning twice a—we see here that a is now 1,

  18. deux fois a ça fait un renvoi de 2.

    so twice a yields a return value of 2.

  19. Où est- ce que je reviens?

    Where do I return to?

  20. Je reviens à l'endroit qui m'avait été appelé,

    I return to where I was called from,

  21. où est- ce que j'avais été appelé? Ici.

    where was I called? Right here.

  22. Donc là maintenant le h deux a correspondant est supprimé et est remplacé.

    So now, the corresponding h(2a) is removed and replaced.

  23. par la valeur de retour qui vaut 2.

    with the return value, which is 2.

  24. Donc là j'ai un appel maintenant à g de 2, donc je passe.

    So now I call g(2), and thus proceed.

  25. à nouveau ici, et j'ai un appel à la fonction g.

    again to this point, and call function g.

  26. Avec comme argument a égale 2.

    with argument a equal to 2.

  27. Je passe à la ligne du dessous, print a,

    I move to the next line and write 'print a,'

  28. donc je vais avoir ici un affichage a qui vaut 2.

    so here I’ll see output showing that a equals 2.

  29. Et je fais un renvoi, return.

    Then I execute a 'return' statement.

  30. Du A, donc je reviens au programme à plan.

    It returns the value of a, so control goes back to the main program.

  31. Mon programme à plan, il était, pardon.

    My main program was… sorry,

  32. Mon programme à plan, il était là.

    my main program was right here.

  33. Donc j'ai ici maintenant, je remplace toutes ces valeurs- là.

    So now, I replace all those placeholders with the return value.

  34. par la valeur de retour qui est 2.

    And that return value is 2.

  35. Donc là j'ai ma valeur de retour,

    Now I have my return value,

  36. je ressors de la fonction g avec la valeur 2 et c'est ici une valeur qui vaut print

    I exit function g with the value 2,

  37. 2. Donc j'ai à nouveau un 2 qui s'affiche sur l'écran.

    and here, 'print' outputs that 2—so another 2 appears on screen.

  38. Puis la fonction, j'ai terminé, j'ai fait le print,

    Then the function finishes—I’ve done the 'print'—

  39. et donc je vais sortir, attention, j'étais encore dans une fonction,

    so I’m about to exit—but note: I was still inside a function,

  40. j'étais à l'intérieur de la fonction qui est la fonction w.

    specifically, inside function w.

  41. Donc je vais sortir de cette fonction w.

    So I’ll exit this function w.

  42. Il n'y a pas de return marqué, mais

    Though no 'return' is explicitly written,

  43. il y a quand même une sortie.

    the function still exits.

  44. Quand il n'y a pas de return,

    When there’s no 'return' statement,

  45. la fonction s'en va quand même et elle ne renvoie rien.

    the function still terminates but returns nothing.

  46. Et rien en Python, c'est None avec un N majuscule.

    In Python, 'nothing' is represented by 'None', with a capital N.

  47. Donc une fonction sans return,

    So a function without a 'return' statement

  48. c'est la même chose qu'une fonction qui retournerait une valeur,

    is equivalent to a function that returns a value,

  49. et cette valeur qui est

    and that value is—

  50. là, elle s'appelle rien.

    in Python, it’s called 'None'.

  51. Le rien existe en Python, c'est la valeur None.

    In Python, 'nothing' exists as the value None.

  52. Et donc quand je vais arriver.

    So, when I reach this point,

  53. à mon programme plan définitif,

    my final version of the program 'plan',

  54. le W21 qui est là va être supprimé par la valeur de retour.

    the W21 shown here will be replaced by the return value.

  55. Cette valeur de retour.

    This return value—

  56. il y en a pas, mais en fait il y en a une, quand il n'y en a pas,

    —seems absent, yet it does exist;

  57. ça veut dire que c'est la valeur None.

    when no value is explicitly returned, it defaults to None.

  58. Et donc quand je vais afficher ici le résultat,

    Therefore, when I print the result here,

  59. il manque une ligne un petit peu en bas.

    one line will be missing near the bottom.

  60. Je vais ici rajouter, ce sera dans le sens où" none" dans les affichages.

    I’ll add it here—so that "None" appears in the output.

  61. Donc, si on veut le voir en Python.

    So, if you want to see it in Python,

  62. Exercice 5. Pardon, d'abord la version 1.1.2.2 nonne qui correspond à ce qu'on avait

    Exercise 5: Sorry—first, version 1.1.2.2,

  63. prévu, et ici.

    the version we originally planned—right here.

  64. Les différentes étapes,

    The different steps—

  65. je vous laisserai les regarder en détail.

    —please review them carefully on your own.

  66. Voilà. Dernier exercice, exercice 6.

    That’s it—final exercise: Exercise 6.

  67. On va dire je le laisse en défi.

    I’ll leave it as a challenge.

  68. Il est même un peu plus compliqué que le 5,

    It’s even slightly more difficult than Exercise 5,

  69. donc voilà, si vous y arrivez.

    so if you manage to solve it—great!

  70. Tout est absolument faisable, mais il est un peu long et c'est.

    Everything is absolutely doable—but it’s somewhat lengthy, and…

  71. Un casse- tête juste pour se casser la tête, on va dire.

    it’s just a brain-teaser—purely for mental exercise.

  72. Si vous avez compris le reste, ça suffit.

    If you’ve understood everything else, that’s already sufficient.

  73. Voilà pour les exos sur les fonctions.

    That concludes the exercises on functions.

  74. That's all, folks!

    That's all, folks!