Opening the book…
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 was last modified on Sun, 18 Sep 2022 02:06:25 GMT and 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.
The book could not be loaded just now.