B1 · Episode 23 · 4:47

Python Function Calls and Return Values

This episode explains how Python functions call each other, how local variables are created and destroyed within function scopes, and how the 'return' statement passes values back to the caller—using step-by-step execution of nested function calls like 'cui_cui' and 'coucou'.

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

Transcript

  1. Maintenant coucou est remplacé

    Now, ‘coucou’ is replaced

  2. par la valeur de retour.

    by the return value.

  3. Donc là mon coucou, ici, il vaut maintenant à la place 5.

    So, my ‘coucou’ here now equals 5.

  4. Donc je suis en train d'exécuter cette instruction- là, c égale 5.

    So I’m executing this statement: c equals 5.

  5. Quel c? Ce c qui est là, je suis à l'intérieur de coucou, donc mon c qui est là,

    Which c? This c here—I’m inside the ‘coucou’ function,

  6. il valait 3, et maintenant ce c là.

    so this c here was 3, and now this c,

  7. Il vaut 5. Et quand je vais passer à l'instruction d'après, return c,

    its value is now 5.

  8. je vais renvoyer de la fonction où j'étais,

    And when I move to the next instruction, ‘return c,’

  9. donc c'est- à- dire cette fonction- là, la cui- cui.

    I’ll return from the function I’m in—that is, this ‘cuicui’ function.

  10. Je vais renvoyer la valeur

    I’ll return the value

  11. 5 au mon programme principal.

    5 to the main program.

  12. Donc là ici, le cucu de 3 qui est là

    So, this ‘cucu(3)’ call here

  13. est remplacé par la valeur de retour, cette valeur de retour qui est 5.

    is replaced by the return value—this value, 5.

  14. Et donc j'ai un magnifique print 5.

    And thus, I get a beautiful print: 5.

  15. Donc là, mon écran, je l'avais pas encore fait.

    Just now, I hadn’t shown the screen yet.

  16. ça affiche juste 5.

    It simply displays 5.

  17. Donc là on a un appel de fonction et vous voyez qu'à chaque fois que j'ai un appel

    So here we have a function call—and as you can see, each time I call a function,

  18. de fonction qui appelle une fonction, je décale.

    and that function itself calls another, the indentation increases.

  19. Et là ce qu'on voit c'est qu'on a la

    What we see here is

  20. variable c qui est dans cuicui.

    the variable c defined inside the ‘cuicui’ function.

  21. Là ici on a une variable a, b qui est créée à l'intérieur de coucou et puis qui va

    Here, variables a and b are created inside ‘coucou’,

  22. disparaître. qui est terminée à partir du moment où je sors de coucou.

    and they vanish once I exit ‘coucou’.

  23. On peut aller vérifier.

    We can verify this.

  24. sur le Python, on devrait avoir un affichage qui vaut juste 5,

    Running it in Python should display just 5,

  25. c'est le cas. Et ensuite le faire en exécutant ici les différentes étapes.

    and indeed it does. Next, let’s step through the execution.

  26. Donc là c'est juste les déclarations des fonctions.

    So, here we only have the function declarations.

  27. Là je rentre dans l'appel à la fonction cui cui.

    Next, I enter the call to the function 'cui cui'.

  28. La valeur qui est donnée, c'est 3, donc c vaut 3.

    The value passed in is 3, so c equals 3.

  29. J'ai un appel maintenant à la fonction coucou.

    Now, I make a call to the function 'coucou'.

  30. Donc là ici, ça se lit comme ça,

    Right now, this part should be read as follows:

  31. le programme principal a appelé la fonction cui cui qui a appelé la fonction coucou.

    the main program called 'cui cui', which in turn called 'coucou'.

  32. Et là on a ici variable a qui vaut 3, b qui vaut 2.

    Here, variable a equals 3 and b equals 2.

  33. On fait nos opérations, a qui vaut 5, et on renvoie la valeur de

    We perform operations: a becomes 5, then we return a's value.

  34. a. Et maintenant, le c prend la valeur de retour de la fonction précédente.

    Then, c receives the return value of the previous function.

  35. Et le print affiche 5 et c'est terminé.

    Finally, print displays 5, and the program ends.

  36. A few moments later.

    A few moments later.

  37. Alors, nouvel exo, exercice 4,

    Alright, here’s a new exercise—Exercise 4,

  38. là ici. On complique petit à petit.

    right here. We’re gradually increasing the difficulty.

  39. Donc essayez de le faire, à vous de jouer.

    Try solving it yourself—you're up!

  40. Faut y aller doucement là sur celui- là.

    Go slowly on this one.

  41. Alors la correction, sur,

    Now for the solution:

  42. on départ, par de là, là ici on est sur des définitions de fonctions,

    We start here—these are function definitions,

  43. donc la première exécution se passe exactement ici.

    so the first execution happens exactly here.

  44. Et alors là il faut bien décomposer, puisque on a.

    Then, we must carefully break it down, since we have:

  45. g de f de g. Ce qu'il faut savoir c'est qu'on commence toujours par le plus

    g of f of g. Remember: we always start from the innermost part.

  46. intérieur. Donc là, parenthèse la plus intérieure ici,

    The innermost parentheses here are:

  47. ça veut dire on va appeler g de

    that means we’ll first call g,

  48. 2. Donc là ici, je vais.

    with argument 2.

  49. Faire un appel avec g ici,

    So, I’ll make a call to g here,

  50. et je l'appelle avec l'argument 2, donc a vaut 2.

    passing in 2 as the argument—so a equals 2.

  51. Donc si j'écris ça, je pars de mon programme principal.

    So, if I write this, I start from my main program.

  52. Et je vais appeler la fonction g

    Then I call the function g.

  53. avec ici a qui vaut 2.

    Here, the parameter a equals 2.

  54. L'étape d'après, c'est de renvoyer trois fois a.

    The next step is to return three times a.

  55. Donc je reviens de cette fonction.

    So, I return from this function.

  56. Avec la valeur 3 fois 2, ça fait 6.

    The returned value is 3 times 2, which is 6.

  57. Donc je reviens dans mon programme principal qui était là.

    I return to my main program, where I was before.

  58. Maintenant ça veut dire que le g de 2 qui est là, il n'existe plus.

    Now, this g(2) here no longer exists.

  59. Il est remplacé par la valeur de la fonction,

    It is replaced by the function’s return value.

  60. la valeur de retour de la fonction.

    That is, the function’s return value.

  61. Maintenant c'est un appel à f.

    Next is a call to function f.

  62. Donc je pars d'ici et c'est un appel avec f et la valeur qui est appelée à

    I start from here and call f, passing the value inside the parentheses as an argument.

  63. l'intérieur c'est la valeur 6.

    The value passed inside is 6.

  64. Donc là ici ça fait un

    So here, we make a

  65. appel à. f et la valeur du paramètre qui est là,

    call to function f,

  66. c'est la seule paramètre qui est donné, c'est b.

    and its only parameter is b.

  67. est égal à 6. Et la fonction va directement renvoyer.

    b equals 6. The function returns directly.

  68. un deux fois b, donc là ici, je renvoie à mon programme principal,

    It returns 2 times b to the main program,

  69. un deux fois six, ça fait douze.

    so 2 times 6, which is 12.