Function arguments are required in Python. So a standard function definition that defines two parameters requires two arguments passed into the function.

# Function definition with two required parameters def create_user(username, is_admin): create_email(username) set_permissions(is_admin) # Function call with all two required arguments user1 = create_user('johnny_thunder', True) # Raises a "TypeError: Missing 1 required positional argument" user2 = create_user('djohansen')

Above we defined our function, create_user, with two parameters. We first called it with two arguments, but then tried calling it with only one argument and got an error. What if we had sensible defaults for this argument?

Not all function arguments in Python need to be required. If we give a default argument to a Python function that argument won’t be required when we call the function.

# Function defined with one required and one optional parameter def create_user(username, is_admin=False): create_email(username) set_permissions(is_admin) # Calling with two arguments uses the default value for is_admin user2 = create_user('djohansen')

Above we defined create_user with a default argument for is_admin, so we can call that function with only the one argument 'djohansen'. It assumes the default value for is_admin: False. We can make both of our parameters have a default value (therefore making them all optional).

# We can make all three parameters optional def create_user(username=None, is_admin=False): if username is None: username = "Guest" else: create_email(username) set_permissions(is_admin) # So we can call with just one value user3 = create_user('ssylvain') # Or no value at all, which would create a Guest user user4 = create_user()

Above we define the function with all optional parameters, if we call it with one argument that gets interpreted as username. We can call it without any arguments at all, which would only use the default values.



In script.py there is a function called make_folders(). We want to add a default argument to the nest parameter in make_folders().

Set it so that if we call make_folders() without an argument for nest the function assumes it gets a value of False.

Folder Icon

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Already have an account?