Skip to main content
Engineering LibreTexts

22.9: Returning Nothing At All

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

    Does it ever make sense for a function to return nothing? Oddly, yes: if it has a useful side effect. One side effect is printing:

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

    def cheer_for(team):

    if team != "Christopher Newport":

    print("Go {} go!!".format(team))

    else:

    print("Uhh...no.")

    cheer_for("Mary Washington")

    cheer_for("Lady Eagles")

    cheer_for("Christopher Newport")

    Output:

    Screen Shot 2022-09-13 at 9.41.10 PM.png

    Here, instead of “something = cheer_for(...)” we type plainold “cheer_for(...)”. That’s because there’s no return value to capture, so there’s no point in setting it equal to anything. We call the function just to enjoy its messages.


    This page titled 22.9: Returning Nothing At All 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.