Skip to main content
Engineering LibreTexts

22.4: Naming arguments

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

    Speaking of arguments, here’s the next thing many students have trouble with. The names of the variables that your main program passes to the function are normally not the same as the arguments defined by the function itself.

    What? Yeah.

    Consider this example:

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

    jets_touchdowns = 1

    jets_field_goals = 3

    jets_total = football_score(jets_touchdowns, jets_field_goals)

    colts_tds = 1

    colts_fgs = 0

    colts_total = football_score(colts_tds, colts_fgs)

    print("The Jets won {} to {}.".format(jets_total, colts_total))

    | The Jets won 16 to 7.

    This code contains two calls to the football_score() function. In the first call, the variables jets_touchdowns (with value 2) and jets_field_goals (0) were passed. In the second, colts_tds (1) and colts_fgs (3) were. In neither case were the arguments literally named “num_tds” and “num_fgs”, which were the function’s own argument names.

    To be crystal clear: whenever football_score() is called, two arguments are passed to it. The function chooses to name the first one it receives “num_tds” and the second one it receives “num_fgs”. But these are its own personal names. They normally have nothing to do with what the calling code chooses to name them.

    Why does it work this way? Perhaps this example makes clear the reason. If the main program had to name its variables exactly the same as the (indented) function did, then the function would not be reusable. In order for it to be called with different values in different contexts, there needs to be this flexible decoupling of variable names.

    To reinforce the lesson, note too that you can call a function without even having variables in the main program at all:

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

    x = football_score(5, 2)

    print("Some mythical team scored {} points today.".format(x))

    Here we literally passed the values 5 and 2 to the function, instead of creating variables to hold them. The function doesn’t mind: it just says, “hey man, whatever’s given to me in slot #1, I’m going to name ‘num_tds,’ and whatever’s delivered through slot #2, I’m going to call ‘num_fgs.’ I don’t care what the outside world’s variable names are...or even whether they have names at all. I just work here.”


    This page titled 22.4: Naming arguments 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.