Functions Multiple Arguments

journey into python

Helpful links

What Are Function Arguments

An argument is a value that is supplied to a function when it is called. Arguments may be variables, values, or objects passed to functions or methods as input. We pass arguments when we call the function.

Single Argument Function

def helloword(word):
  return f'Hello {word}'
print(helloword(word='JourneyIntoPython'))
Hello JourneyIntoPython

Multiple Argument Function

def sum(x,y):
  return x + y
print(sum(1,2))
3

Argument Forms in Python

Types of Argument Forms

  • Default
  • Keyword
  • Special
  • Arbitrary

Default Values in Functions

  • Order is important
  • Default arguments are optional
  • Any argument without a default values is required
  • Don’t mix; default arguments should follow non-default arguments within the function
  • Any number of default arguments may be specified for the function
  • The assignment operator = is used to assign a default value to the argument
  • Supplying a default value for the arguments in function calls replaces the default value
# Oversimplified Example
def sum(x,y,z=5):
  return x+y+z

a=sum(1,2)
b=sum(1,2,3)
print(f'Example A: {a}\nExample B: {b}')
Example A: 8
Example B: 6
# Realworld Example: Calculate A Monthly Car Payment
def loan_calc(principal, rate=3.65, tax=5.75, months=60):
  r = ((rate/100)/12)
  numerator = (r *((1+r)**(months)))
  denominator = ((1+r)**(months)) - 1
  return principal*(1+(tax/100)) * (numerator/denominator)

p=30000
x=loan_calc(p)
y=loan_calc(p,12)
print(f'Average Interest Montly Payment: ${int(x)}\nHigh Interest Monthly Payment: ${int(y)}')
Average Interest Montly Payment: $579
High Interest Monthly Payment: $705

Using Keywords in Functions

  • When using keyword arguments you can specify the argument name
  • Order no longer matters if the argument name is specificied
  • All keywords given must match available arguments
# Using the example function above

p=30000
y=loan_calc(rate=12, principal=p)
z=loan_calc(12, p) # Note that we are not using the default positions or the keyword
print(f'Correct High Interest Monthly Payment: ${int(y)}\nIncorrect High Interest Monthly Payment: ${int(z)}\n\nOrder matters, but only when you are not using keywords.')
Correct High Interest Monthly Payment: $705
Incorrect High Interest Monthly Payment: $317

Order matters, but only when you are not using keywords.

Using Special Arguments in Functions

Arguments can be passed to a Python function by position or expressly by keyword. It makes sense to limit the manner in which arguments can be supplied so that a developer simply looks at the function definition to see if things are passed by position, position, or keyword, or keyword.

Examples Here

Using Arbitrary Arguments in Functions

  • A function can be called with an arbitrary number of variables
  • Arguments are wrapped in a tuple
def hello_names(*names):
  return f'Hello {", ".join(names)}'

hello_names('John', 'Joe', 'Dan')
'Hello John, Joe, Dan'

More details on the types of arguments

Learning Summary

  • Multiple Arguments
    • Python makes it easy to have multiple variables with different data types
  • Default Arguments
    • Python allows you to set arguments with default values
    • Argument order matters
  • Keyword Arguments
    • Using keywords improves code readability
    • Order is no longer an issue when specifying arguments by keyword
  • Special Arguments
    • You can restrict how arguments are passed using special arguments with / or *
  • Arbitrary Arguments
    • You can pass an arbitrary number of arguments using the *

Leave a Comment