Creating Functions With No Input Parameters
For more information on concepts covered in this lesson, you can check out Python Scope & the LEGB Rule: Resolving Names in Your Code.
00:00 Defining Functions With No Input Parameters. In this course, you’ll use the example of a basic program that creates and maintains a shopping list and prints it out when you’re ready to go to the supermarket.
00:14 Start by creating a shopping list.
00:29 You’re using a dictionary to store the item name as the key and the quantity you need to buy of each item as the value. You can define a function to display the shopping list.
00:54 When you run this script, you’ll get a printout of the shopping list, as seen on-screen.
01:07 The function you’ve defined has no input parameters, as the parentheses in the function signature are empty. The signature is the first line in the function definition.
01:18
You don’t need any input parameters in this example, since the dictionary, shopping_list
, is a global variable. This means it can be accessed from everywhere in the program, including from within the function definition. This is called the global scope.
01:34 You can read more about scope in this Real Python tutorial. Using global variables in this way is not a good practice. It can lead to several functions making changes to the same data structure, which can lead to bugs that are hard to find.
01:51 You’ll see how to improve on this later on in this course, when you pass the dictionary to the function as an argument. In the next section, you’ll define a function that requires an input argument.
Become a Member to join the conversation.