CPSC120A
Fundamentals of Computer Science I

Activity 6

Exploring Function Parameters.

Default Parameters and Named Arguments

Hopefully the idea of functions is getting a little bit easier to grasp at this point. You are going to have plenty of time today to practice writing your own functions. However, there are a few neat things that you can do with functions to make them more useful. This activity will explore these ideas.

Named Arguments

Write the following function in an Python program:

def print_user_information(name, email, cell):
    print("Username:", name)
    print("Email:", email)
    print("Cell:", cell)
  1. What does the following function call print? print_user_information("Scotty", "chssmith@roanoke.edu", "867-5309")

  2. What does the following function call print? print_user_information("chssmith@roanoke.edu", "Scotty", "867-5309")

  3. What does the following function call print? print_user_information(name="Scotty", email="chssmith@roanoke.edu", cell="867-5309")

  4. What does the following function call print? print_user_information(cell="867-5309", email="chssmith@roanoke.edu", name="Scotty")

  5. What is an advantage to using named arguments?

Default Parameters

  1. What does the following function call print? print_user_information("Scotty", "chssmith@roanoke.edu")

  2. Change the program to the following code. What does the above function call do now?

    def print_user_information(name, email, cell="Not Given"):
        print("Username:", name)
        print("Email:", email)
        print("Cell:", cell)
    
  3. Given the previous result, consider the following change:

    def print_user_information(name="No Name", email, cell="Not Given"):
        print("Username:", name)
        print("Email:", email)
        print("Cell:", cell)
    

    What does the function call print_user_information("chssmith@mail.roanoke.edu") print?

  4. Given the above results, what are the restrictions on creating default parameters?