Skip to main content
Engineering LibreTexts

22.3: Writing vs. calling

  • Page ID
    88719
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Now here’s one of the most perplexing things for beginners. Consider this code:

    Code \(\PageIndex{1}\) (Python):

    team_name = "Broncos"

    num_tds = 3

    num_fgs = 2

    def football_score(num_tds, num_fgs):

    return num_tds * 7 + num_fgs * 3

    It surprises many to learn that this code snippet does not compute anything, football score or otherwise. The reason? We only wrote a function; we didn’t actually call it.

    This is sort of like building an impressive machine but then never pushing the “On” button. The above code says to do four things:

    1. Create a team_name variable and set its value to the string "Broncos".
    2. Create a num_tds variable and set its value to the integer 3.
    3. Create a num_fgs variable and set its value to the integer 2.
    4. Create a function called football_score which, if it is ever called in the future, will compute and return the score of a football game.

    In other words, that last step is just preparatory. It tells Python: “by the way, in case you see any code later on that calls a function named ‘football_score,’ here’s the code you should run in response.”

    To actually call your function, you have to use the same syntax we learned on p. 20, namely:

    Code \(\PageIndex{2}\) (Python):

    team_name = "Broncos"

    num_tds = 3

    num_fgs = 2

    the_score = football_score(num_tds, num_fgs)

    print("The {} scored {} points.".format(team_name, the_score))

    | The Broncos scored 27 points.

    Follow the thread of execution closely here. First, the three variables are created, in what I’ll often call “the main program.” By “main,” I really just mean the stuff that’s all the way flush-left, and thus not inside any “def.” It’s the main program in the sense that when you execute the cell, it’s what immediately happens without needing to be explicitly called.

    Then, after those three variables are created, the football_score() function is called, at which point the flow of execution is transferred to the inside of the function. Since this simple function has only one line of code in its body (the return statement), executing it is really quick; but it’s still important to realize that for a moment, Python isn’t “in” that Broncos cell at all. Instead it jumps to the function, carries out the code inside it, and then returns the value...

    ...right back into the waiting arms of the main program, which stores that returned value (an integer 27, as it turns out) in a new variable named the_score. Then the flow continues, and the print() statement executes as normal.

    Bottom line: every time you want to run your function’s code – whether that’s a hundred times, once, or not at all – you need to call it by typing the name of the function (with no “def”) followed by a banana-separated list of arguments.


    This page titled 22.3: Writing vs. calling is shared under a not declared license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?