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.
Write the following function in an Python program:
def print_user_information(name, email, cell): print("Username:", name) print("Email:", email) print("Cell:", cell)
What does the following function call print?
print_user_information("Scotty", "chssmith@roanoke.edu",
"867-5309")
What does the following function call print?
print_user_information("chssmith@roanoke.edu", "Scotty",
"867-5309")
What does the following function call print?
print_user_information(name="Scotty",
email="chssmith@roanoke.edu", cell="867-5309")
What does the following function call print?
print_user_information(cell="867-5309",
email="chssmith@roanoke.edu", name="Scotty")
What is an advantage to using named arguments?
What does the following function call print?
print_user_information("Scotty",
"chssmith@roanoke.edu")
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)
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?
Given the above results, what are the restrictions on creating default parameters?