Function Defaults


You can set up your Python functions to have default values. Between the round brackets of your function definition, come up with a variable name then type an equals sign.After the equals sign, type a value for your variable. For example, suppose we want our function from the previous lesson to add ten to the answer. But only if a default has been overridden. We could set it up like this:

def addition(first_number, second_number, add_ten=False):

We've called our default variable add_ten. After an equals sign, we've set add_ten to a value of False. We only want to add ten to our answer if the default has been overridden.

We can do that in an If Statement. Change your function to match ours:

def addition(first_number, second_number, add_ten=False):

if add_ten == True:

answer = first_number + second_number + 10


answer = first_number + second_number

return answer

Here it is in the coding window:

Python function with a default value

In case you're wondering, the reason we have a grey underline for add_ten == True is because we could have simplified the line to this:

if add_ten:

You can also use the keyword is instead of the two equal signs:

if add_ten is True

And you can also use not with is:

if add_ten is not True

But what the code does is add 10 to answer only if the add_ten variable is True. But it's False by default so 10 doesn't get added to answer.

To add 10, and override the default, you do it from the calling line. Ours is this, at the moment:

additionTotal = addition(3, 5)

If you run your code, you'll see there are no problems, and the output window will still report an answer of 8.

However, change your calling line to this:

additionTotal = addition(3, 5, True)

Now run your code again. You'll see this:

The addition total was 18

We've typed True in our calling line, after the number 5 and a comma. A value of True then gets sent to the function itself. This True value gets stored in the add_ten variable.

You don't need to include a default value when you're calling a function. Default values are optional. That's why addition(3, 5) still works.

When setting up your functions, you can have as many default values as you want.

def addition(first_number, second_number, add_ten=False, message="Hello World"):

The only values you need when calling a function are the ones without the equals sign. So the first two in the line above are required. You couldn't do this, for example, when calling the function:

additionTotal = addition(3)

This would get you an error because two of the arguments are required, the first two. This wouldn't cause an error, by the way:

additionTotal = addition(3, True)

What would happen is that True would evaluate to 1. A value of 1 would then get stored in the second_number variable and not the add_ten variable.

One last thing about default values. You can't set them up before required arguments. So you can't do this:

def addition(add_ten=False, first_number, second_number):

The required arguments are the ones without equal signs, and they need to come before your defaults.

In the next lesson, you'll see that functions can be called from other functions.

Calling Functions from other Functions >