Creating Functions With Required Parameters
00:00 Defining Functions With Required Input Arguments. Instead of writing a shopping list directly in the code, you can now initialize an empty dictionary and write a function that allows you to add items to the shopping list.
The function iterates through the dictionary’s keys, and if the key exists, the quantity is increased. If the item is not one of the keys, the key is created, and a value of
1 is assigned to it.
01:21 When you call the function, you pass arguments within the parentheses, one for each parameter. An argument is a value that you pass to the function. The distinction between parameters and arguments can often be overlooked.
It’s a subtle but important difference. You may sometimes find parameters referred to as formal parameters and arguments as actual parameters. The arguments you input when calling
add_item() are required arguments.
The traceback will give a
TypeError stating that the arguments are required. You’ll look at more error messages relating to using the wrong number of arguments or using them in the wrong order later on in this course.
Become a Member to join the conversation.