Skip to main content
Engineering LibreTexts

22.10: Calling a Function from Another Function

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

    Finally, note that we can put any code we desire inside a function’s body, including one or more calls to other functions! Check out this bad boy:

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

    def greet(first_n, last_n, middle_i, gender, status, deg, lang):

    sal = salutation(gender, status, deg)

    if lang == "Swedish":

    greeting = "Hej"

    elif lang == "Russian":

    greeting = "Privet"

    elif lang == "Hindi":

    greeting = "Namaste"

    else:

    greeting = "Yo"

    full = full_name(last_n, first_n, middle_i)

    print("{}, {} {}!".format(greeting, sal, full))

    greet("Greta","Thunberg","F","female","single","none","Swedish")

    greet("Maria","Sharapova","S","female","single","none","Russian")

    greet("Garry","Kasparov","K","male","married","BA","Russian")

    greet("Angela","Merkel","D","female","married","PhD","German")

    Output:

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

    In the course of its duties, greet()’s function body calls both salutation() and full_name() for help. They each produce components of its complete solution. Good teamwork!


    This page titled 22.10: Calling a Function from Another Function 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?