Function Arguments and Return Values

CS 65: Introduction to Computer Science I

Arguments we've been passing to functions

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.

In [ ]:
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.

Syntax for defining functions that take arguments

When defining a function that takes arguments, put a new variable name inside the parentheses. This variable is called a parameter.

In [1]:
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.

In [2]:
f_to_c(90)

temp_reading = float(input("What was the reading in fahrenheit? "))
f_to_c(temp_reading)
That's 32.22222222222222 in Celsius
What was the reading in fahrenheit? 40
That's 4.444444444444445 in Celsius

Group Exercise

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?

In [ ]:
def greet_by_name(name):
    print("Hello",name,", I hope you are having an excellent day.")

Things to remember about arguments and parameters

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

In [3]:
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
That's 37.77777777777778 in Celsius

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!

Functions with multiple parameters

You can define functions with multiple parameters by separating the variable names with commas.

In [4]:
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)
In [5]:
calculate_pay(7.25,40)
calculate_pay(52.75,25)
Total pay: 290.0
Total pay: 1318.75

arguments get assigned to parameters in the order they're listed

In [6]:
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.
In [7]:
calculate_pay(52.75,25,7,420.50)

Group Exercise

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.

Getting data out of functions

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:

In [ ]:
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 =

Syntax for returning values

To return a value from a function you've defined, put the return statement somewherer in your function

  • keyword return
  • an expression (i.e., a value, a variable, mathematical operation, or anything that results in a value)
In [8]:
def f_to_c(fahrenheit_temp):
    celsius_temp = (fahrenheit_temp-32)*(5/9)
    return celsius_temp
In [9]:
f_reading = float(input("Enter fahrenheit reading: "))
c_temp = f_to_c(f_reading)
print(c_temp)
Enter fahrenheit reading: 30
-1.1111111111111112

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.

Returned values can be used anywhere the value itself could be used

In [10]:
print("100F is",f_to_c(100),"in Celsius.")
kelvin_reading = 273.15 + f_to_c(100)
print("In Kelvin, it is",kelvin_reading)
100F is 37.77777777777778 in Celsius.
In Kelvin, it is 310.92777777777775

Group Exercise

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()

In [11]:
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()
Enter your hourly wage: 10.25
Enter your number of hours worked: 16
Total pay: 164.0

Why should you return instead of just printing the result?

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.

In [ ]:
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")