So far, all of the example functions that have been used in this book have had either no parameter, or exactly one parameter variable. However, we are not limited to just one. In fact, it is commonplace to have a function that has two, three, four, or even more parameter values (and therefore, arguments) that it needs in order to do its job. The way that we specify these are by putting multiple variable names within the parentheses of the function declaration, separated by commas. When we call a function that has multiple parameters, we can specify multiple argument values at the call location in a similar way: A sequence of values separated by commas.
One example would be a function to calculate taxes. This Function accepts two parameters: An amount of yearly income in dollars, and the amount of tax credits that were given to this person:
def compute_tax(income, credits):
net = income - credits
if net < 50000:
return net * 0.15
elif net < 100000:
return net * 0.20
return net * .25
This function will take the income and credit amount, and subtract the latter from the former. With the leftover number, it will compute a tax. If the net dollar amount is less than 50k, it applies a 15% tax. If between 50k and 100k, it applies a 20% tax. If 100k or above, it applies a 25% tax. The resulting dollar value is what gets returned. To call this, we could do something like:
def main():
salary = int(input('How much money did you make this year? '))
user_credit = int(input('How much tax credit do you have? '))
result = compute_tax(salary, user_credit)
print('You owe $' + str(result) + ' in taxes.')
When the call to compute_tax(salaty, credits)
is made in this code, the income
parameter variable gets assigned the value from the salary
variable, and the credits
parameter gets assigned the value from user_credit
.
Let’s write another finances-related function, but this time with three parameters instead of two. The function will be designed to be used by people who have purchased a home, and have a mortgage (a loan) to pay off the house over the course of several years. The purpose of the function will be to tell a user how much money the monthly payment will be for the mortgage. However, in order to calculate this, the function will need three key pieces of information about the mortgage:
The function will have three parameters, one for each of these values. The function will return the expected amount of money the mortgage payment will be per month. The implementation would look like this:
def calculate_monthly_payment(amount, years, interest_rate):
monthly_interest = interest / 12
months = years * 12
top = amount * ( monthly_interest * (1 + monthly_interest)**months )
monthly_payment = top / ( (1 + monthly_interest)**months - 1 )
return monthly_payment
We can put this all together in one program that helps a user with taxes and mortgage info:
def compute_tax(income, credits):
net = income - credits
if net < 50000:
return net * 0.15
elif net < 100000:
return net * 0.20
return net * .25
def calculate_monthly_payment(amount, years, interest_rate):
monthly_interest = interest / 12
months = years * 12
top = amount * ( monthly_interest * (1 + monthly_interest)**months )
monthly_payment = top / ( (1 + monthly_interest)**months - 1 )
return monthly_payment
def main():
salary = int(input('How much money did you make this year? '))
user_credit = int(input('How much tax credit do you have? '))
mortgage = int(input('How much is your mortgage? '))
years = int(input('How many years is your mortgage? '))
interest = float(input('What is your mortgage interest rate? '))
result = compute_tax(salary, user_credit)
print('You owe $' + str(result) + ' in taxes.')
result = calculate_monthly_payment(mortgage, years, interest)
print('Your mortgage payment should be $' + str(result) + ' per month.')
main()