A7 - Subprograms that accept Arguments
For a long time, you've been working with built-in subprograms, which we've been calling functions or methods. Most of the time, when we call functions, we provide a list of arguments inside the brackets. A good example of this is the print function, which will accept any number of arguments:
When we create our own subprograms, we can also accept arguments. Here is a subprogram that will output a text based weather report. Following the definition of the subprogram, there are two calls made to it with different sets of arguments.
def print_weather_report(date, high, low, prob_precip):
print("-------------------------------------------------------------------")
print("WEATHER REPORT FOR: ", date, "\n")
print(" High: ", high)
print(" Low: ", low)
print(" Change of Precipitation: ", prob_precip, "%")
print("-------------------------------------------------------------------")
print_weather_report("April 29, 2025", 23, 5, 70)
print_weather_report("April 30, 2025", 12, 1, 30)
The output of this program is as follows:
-------------------------------------------------------------------
WEATHER REPORT FOR: April 29, 2025
High: 23
Low: 5
Change of Precipitation: 70 %
-------------------------------------------------------------------
-------------------------------------------------------------------
WEATHER REPORT FOR: April 30, 2025
High: 12
Low: 1
Change of Precipitation: 30 %
-------------------------------------------------------------------
Notice that when the weather report subprogram was defined, we listed a set of four values inside the brackets:
This means that when the subprogram is called, four arguments will be expected, and their values will be stored in the variables
date, high, low, and prop_precip.
We can then use these variables (in this scenario, also known as parameters) in any way we'd like.
In our example, we insert the information into the weather report template.
Your Task
Create a blank file in your Unit-3 folder called a7.py.
Inside the file, create your own subprogram that accepts arguments, similar to the weather reporting program. Then, also like the weather reporting program, clearly display the information by inserting the values into the text you output.
Some ideas of what you could do:
- Accept as arguments the stats from a sports match, and clearly output this information.
- Accept as arguments some car specifications, and clearly output this information.
- Aceept as arguments some information about a person (eg, age, height, weight), and clearly output this information
Note - to be clear, you just need to choose ONE of these, or an idea of your own.