Salvatore Lopiparo

Software Engineer

Super Simple Python #12 – Function Arguments

No, this is not about a function sleeping on the couch because it told another function what it really thought about that dress. Function arguments are the variables that you pass to a function. There are two basic kinds: arguments and keyword arguments.

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

The first kind, arguments, are what we have used thus far for passing variables to a function. These types of arguments are required to use the function.

def greeting(first_name, last_name):
    print("Hello " + first_name + " " + last_name + "!")
    print("How are you today?")

They are also ordered, meaning in our example that first_name must come first, followed by last_name.

>>> my_first_name = "Frank"
>>> my_surname = "Sinatra"
>>> greeting(my_first_name, my_surname)
Hello Frank Sinatra!
How are you today?

The other type of function arguments, keyword arguments, are not required. They let you have a default argument that can be changed if the calling function wants. These are denoted by giving the name of the keyword argument followed by the default value to use. name_of_dog is our keyword argument in this snippet, with an empty string being the default value.

def greeting(first_name, last_name, name_of_dog=""):
    print("Hello " + first_name + " " + last_name + "!")
    if name_of_dog:
        print("Hello to your dog, " + name_of_dog + ", too!")
    print("How are you today?")

This will allow us to call the function with or without name_of_dog, so we don’t have to write a separate function for each case (such as, greeting_with_dog and greeting_without_dog)!

>>> greeting("Frank", "Sinatra", name_of_dog="Fido")
Hello Frank Sinatra!
Hello to your dog, Fido, too!
How are you today?
>>> greeting("George", "Clooney")
Hello George Clooney!
How are you today?

Keyword arguments are not ordered. You can pass the keyword arguments in any order (as long as all the ordered arguments are first!). You can also skip some keyword arguments if you want to keep the default values.

def greeting(first_name, last_name, name_of_dog="", name_of_cat="", name_of_bird=""):
    print("Hello " + first_name + " " + last_name + "!")
    if name_of_dog:
        print("Hello to your dog, " + name_of_dog + ", too!")
    if name_of_cat:
        print("Your cat, " + name_of_cat + ", is looking sassy!")
    if name_of_bird:
        print("And let's not forget your bird, " + name_of_bird + "!")
    print("How are you today?")
>>> first_name = "Michael"
>>> last_name = "Jordan"
>>> 
>>> name_of_cat = "Sylvester"
>>> name_of_bird = "Tweety"
>>> 
>>> greeting(first_name, last_name)
Hello Michael Jordan!
How are you today?
>>> greeting(first_name, last_name, name_of_bird=name_of_bird, name_of_cat=name_of_cat)
Hello Michael Jordan!
Your cat, Sylvester, is looking sassy!
And let's not forget your bird, Tweety!
How are you today?

I have the sudden urge to watch Space Jam.

DROP A COMMENT

Your email address will not be published. Required fields are marked *