Passing data between functions
A program made of several functions needs to move data between them. The module on strings and lists showed one way: through parameters and return values. Now that you know global, there's a second way: global variables that all the functions read and change. The first way is almost always the better one.
With parameters and return values, each function is an independent whole. What it needs comes in as arguments, and what it produces goes out with return. You can read the function and understand it without the rest of the program, test it by calling it with different arguments, and use it in another program. When many functions change the same global variables, any of them can change the data at any time, and finding which one caused a wrong value means reading all of them.
Here is a program in three functions. The main program keeps the data, the list temperatures, and hands it to each function that needs it:
def read_temperatures(days):
temperatures = []
for day in range(1, days + 1):
temperatures.append(int(input(f"Day {day}: ")))
return temperatures
def warm_days(temperatures):
count = 0
for temperature in temperatures:
if temperature >= 20:
count += 1
return count
def report(temperatures):
print("Highest:", max(temperatures))
print("Lowest:", min(temperatures))
print("Warm days:", warm_days(temperatures))
temperatures = read_temperatures(4)
report(temperatures)
18
22
25
17Day 1: 18
Day 2: 22
Day 3: 25
Day 4: 17
Highest: 25
Lowest: 17
Warm days: 2read_temperaturescreates the list and returns it. The main program stores it in its own variable.reportgets the list as an argument, and passes it on towarm_days, which returns a number.- None of the functions uses a global variable. The parameter
temperaturesin each function is a local name for the list the caller gave it.
A main function
You can go one step further and put the main program in a function too, usually called main. Then the program has no global variables at all, and the line under if __name__ == "__main__": only starts it:
def main():
temperatures = read_temperatures(4)
report(temperatures)
if __name__ == "__main__":
main()
Now temperatures is a local variable of main. The only way other functions can get it is as an argument, which is exactly the point.
A list is passed, not copied
One thing to remember from the module on references: a list given as an argument is the same list, not a copy. If a function changes it with append or by assigning to an index, the caller sees the change. That's useful when changing the list is the function's job. When it isn't, a function should build a new list and return it.