Skip to main content
Engineering LibreTexts

22.7: Returning True or False

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

    It’s common for a programmer to want a function which, instead of returning a number or text, tells her whether or not something is true. This lets her use the return value of such a function as the condition of an if statement.

    Here’s a trivial example:

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

    def is_old_enough_to_vote(age):

    if number >= 18:

    return True

    else:

    return False

    x = is_old_enough_to_vote(13)

    if x:

    print("Yes, a 13-year-old can vote!")

    else:

    print("Alas, a 13-year-old must wait.")

    if is_old_enough_to_vote(19):

    print("Yes, a 19-year-old can vote!")

    else:

    print("Alas, a 19-year-old must wait.")

    | Alas, a 13-year-old must wait.

    | Yes, a 19-year-old can vote!

    The values True and False are called boolean values, after the 19th-century mathematician George Boole. Note that in Python they must begin with capital letters.

    The somewhat odd-looking “if x:” line is possible because x was set to the return value of a call to is_old_enough_to_vote(), and that function returned a boolean value.


    This page titled 22.7: Returning True or False 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.