Skip to main content
Engineering LibreTexts

22.8: Multiple Return Statements

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

    Another thing the “old enough to vote” example illustrates is the presence of more than one return statement in a function. You might have thought this was useless, since on p. 225 I mentioned that as soon as a return is encountered during execution, the function is immediately completed. Why, then, would one ever have more than one – the second one could never be reached, right? Wrong. The branching nature of the if/else statement (above) means that the first return will be skipped over in some situations (negative arguments, etc.) so it’s perfectly sensible to have more than one.

    A more complicated example would be the “salutation” algorithm from section 21.4 (p. 217), this time embodied in a function:

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

    def salutation(gender, marital_status, degree):

    if degree == "PhD" or degree == "MD":

    return "Dr."

    elif gender == "male":

    return "Mr."

    elif gender == "female":

    if marital_status == "married":

    return "Mrs."

    elif marital_status == "single":

    return "Miss"

    else:

    return "Ms."

    else:

    return "Mx."

    my_salutation = salutation("male", "married", "PhD")

    print("Why hello, {} Davies.".format(my_salutation))

    print("And hello, {} Davies.".format(salutation("female", "married", "BS")))

    | Why hello, Dr. Davies.

    | And hello, Mrs. Davies.

    Wow, all that code is in one function? Yeah. That’s not unusual at all, although you should strive to make functions as compact as they can be. (The salutation() function is as compact as it can be, actually: there’s no way to shorten it without changing what it does.)

    The salutation() function, as you can see, has a veritable crap-ton of return statements – six in fact. But only one will ever be reached, because as soon as one is reached, the function is officially finished, and returns that value.


    This page titled 22.8: Multiple Return Statements 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.