In most of the functions we've called, we've passed arguments to the them data that we put inside of the parentheses that the function needs to work as intended.
print("Hello world!")
type(2.70)
fahrenheit_temp = int( input("Enter the temperature in Fahrenheit ") )
len(name)
user_input = int( input("Enter a number greater than "+str(target_number)+" ") )
max(rainfall_amounts)
with open("gettysburg.txt") as gettysburg_file:
gettysburg_text = gettysburg_file.readlines()
"Hello world!" tells the print() function what should be displayed
2.70 tells the type() function what we want to know the type of
"Enter the temperature in Fahrenheit " tells the input() what the prompt should be
name is the string variable we want to know the length of with len()
etc.
When defining a function that takes arguments, put a new variable name inside the parentheses. This variable is called a parameter.
def f_to_c(fahrenheit_temp):
celsius_temp = (fahrenheit_temp-32)*(5/9)
print("That's",celsius_temp,"in Celsius")
When we call the function, whatever argument we give it gets saved to the parameter.
f_to_c(90)
temp_reading = float(input("What was the reading in fahrenheit? "))
f_to_c(temp_reading)
Consider the following function that includes a parameter. Write some code that calls this function and supplies an appropriate argument.
Discuss: name is a variable, but it doesn't look like it ever gets assigned a value. When does the assignment actually happen?
def greet_by_name(name):
print("Hello",name,", I hope you are having an excellent day.")
Parameter: a variable - you never put a literal number/string/etc. in the parens when you define the function.
Argument: can be a literal value, a variable, or any other expression (it will be evaluated and the result will be passed as the argument)
Remember: The value of the argument gets copied to the parameter variable
def f_to_c(fahrenheit_temp): #fahrenheit_temp is a parameter
celsius_temp = (fahrenheit_temp-32)*(5/9)
print("That's",celsius_temp,"in Celsius")
f_to_c(90+10) #90 is an argument
To make things worse, even experienced programmers will sometimes use these terms interchangeably. You're not allowed to do that until you really really thoroghly understand how functions, arguments, and parameters work!
You can define functions with multiple parameters by separating the variable names with commas.
def calculate_pay(wage,hours):
if hours <= 40:
pay = wage*hours
else:
overtime_hours = hours-40
pay = (wage*40) + (wage*1.5*overtime_hours)
print("Total pay:",pay)
calculate_pay(7.25,40)
calculate_pay(52.75,25)
arguments get assigned to parameters in the order they're listed
def calculate_pay(wage,hours,w4_dependents,health_insurance_contribution):
if hours <= 40:
pay = wage*hours
else:
overtime_hours = hours-40
pay = (wage*40) + (wage*1.5*overtime_hours)
#pretend we have all the code here to adjust paycheck for withholdings, etc.
calculate_pay(52.75,25,7,420.50)
Define a new function called print_face with three parameters (make sure to use descriptive names for the parameter variables) that would allow me to call it and get the following behavior.
Parameters: Enable getting data into functions
Return values: Enable getting data out of functions - think of it as the result of a function
Let's think about examples from functions we've called:
name = input("What is your name? ")
name_length = len(name)
print(name,"has",name_length,"letters")
input() returns a string (whatever the user typed) and we saved that result to name
len() returns an integer (the length of the string name) and we saved that result to name_length
print() doesn't return anything - we never see print on the right side of an =
To return a value from a function you've defined, put the return statement somewherer in your function
returndef f_to_c(fahrenheit_temp):
celsius_temp = (fahrenheit_temp-32)*(5/9)
return celsius_temp
f_reading = float(input("Enter fahrenheit reading: "))
c_temp = f_to_c(f_reading)
print(c_temp)
Because f_to_c() returns something, we can save its result to c_temp
Note that the return statement will cause the function to end - even if it is in the middle of the function, so you usually find it at the end of a function.
print("100F is",f_to_c(100),"in Celsius.")
kelvin_reading = 273.15 + f_to_c(100)
print("In Kelvin, it is",kelvin_reading)
Change the program below so that pay is returned from calculate_pay() instead of printed.
Change the user_pay_calculator() function so that it correctly calls the new version of calculate_pay()
def calculate_pay(wage,hours):
if hours <= 40:
pay = wage*hours
else:
overtime_hours = hours-40
pay = (wage*40) + (wage*1.5*overtime_hours)
print("Total pay:",pay) #change this line
def user_pay_calculator():
hourly_wage = float(input("Enter your hourly wage: "))
num_hours = float(input("Enter your number of hours worked: "))
calculate_pay(hourly_wage,num_hours) #change this line
user_pay_calculator()
Returning values gives you more flexibility in how the function can be used.
Consider the pay calculator. We might want to allow a user to interact with it to check their pay; or, we might read employee information from a file so that we can process all their paychecks.
Good programming practice: don't put print and input statements inside of most functions - move all the user interaction to its own function.
import csv
#make sure to include the calculate_pay() function you wrote above
def process_payroll(employee_hours_logged_filename):
with open(employee_hours_logged_filename) as hours_log_file:
employees_log = csv.reader(hours_log_file)
employees_log = list(employees_log)
counter = 0
while counter < len(employees_log):
print(employees_log[counter][0],":",calculate_pay(float(employees_log[counter][1]),float(employees_log[counter][2])))
counter += 1
process_payroll("emp_hours_log_2021_June20-26.csv")