Skip to main content
Engineering LibreTexts

22.5: Passing Aggregate Data to Functions

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

    Even though the previous example involved passing atomic data to a function, you can totally pass aggregate data as well. Suppose we’d like to be able to easily compute the IQR (recall p. 150) of a univariate data set. Writing a function to do that is a snap:

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

    def IQR(some_data):

    return some_data.quantile(.75) - some_data.quantile(.25)

    We can now call it on anything we like, like our examples from p. 150 and p. 159:

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

    print("The IQR of the YouTube plays data is {}".format(IQR(num_plays)))

    print("The IQR of the NCAA scoring data is {}".format(IQR(pts)))

    | The IQR of the YouTube plays data is 412.

    | The IQR of the NCAA scoring data is 15.

    Again, we named the function’s own argument (some_data) something different than the variables it was called with (num_plays first, and then pts). This is a happy and healthy thing.


    This page titled 22.5: Passing Aggregate Data to Functions 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?