Skip to main content
Engineering LibreTexts

22.2: The 'def' statement

  • Page ID
    88718
  • \( \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}}\)

    Okay, down to brass tacks. The way to create (not call) a function in Python is to use the def statement. For our first example, let’s write a function to compute an (American) football team’s score in a game:

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

    def football_score(num_tds, num_fgs):

    return num_tds * 7 + num_fgs * 3

    For those not familiar with football scoring, each “touchdown” (or TD for short) a team scores is worth seven points, and each “field goal” (or FG) is worth three. (For those who are familiar with football scoring, please forgive the simplifications here – extra point kicks, safeties, etc. It’s a first example.)

    As you can see from the code snippet, above, the word def (which stands for “define,” since we’re “defining” – a.k.a. writing – a function) is followed by the name of our function, which like a variable name can be any name we choose. After the name is the list of arguments to the function, in bananas.

    All that is the header of the function. The body, like other “bodies” we’ve seen (p. 137, p. 212) is indented underneath. The football_score body is just one line long, but it can be as many lines as necessary.

    Finally, we see the word “return” on that last line. This is how we control the return value which is given back to the code that called our function (review section 5.3 on p. 38 if you need a refresher on this). Whenever a return statement is encountered during the execution of a function, the function immediately stops executing, and the specified value is handed back to the calling code. More on that in a minute.


    This page titled 22.2: The 'def' statement 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.