Skip to content
Home » Functions in Python – Optional Parameters

Functions in Python – Optional Parameters

Spread the love

Here’s another article in the Functions in Python series, this time about optional parameters.

If you haven’t read the previous parts yet, feel free to do so. Here they are:

1) Introduction to Functions

2) Functions with Parameters

3) The return Statement

4) Mandatory Parameters

Apart from mandatory parameters, there are also optional parameters. Let’s talk about them next.

Optional parameters are also called default parameters. In the function definition optional parameters are assigned default values. When we call the function, we don’t have to pass the arguments corresponding to the optional parameters, which will be understood as passing the arguments with their default values. If we want to use different values for the optional parameters, we can use them just like the mandatory ones.

Here’s an example with just one parameter, which is an optional one:

def countdown(downfrom = 10):
    for count in range(downfrom, -1, -1):
        print(count, end = " ")
    print()

countdown()

countdown(17)

The parameter downfrom is optional. We can tell it by the fact that it is assigned a default value in the function definition. The default value is 10. If we call the function without any arguments, the default value will be used and the function will count down from 10 to 0. This is what happens when we call the function for the first time.

If we want to use a different value at which the countdown is to start, we just pass it as an argument and it will overwrite the default value. That’s what we’re doing in the second call. Here’s the output:

10 9 8 7 6 5 4 3 2 1 0
17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Both Mandatory and Optional Parameters in Function Definition

If there are both mandatory and default parameters, all the mandatory parameters must come before the optional ones. Here’s an example:

def full_name(first_name, last_name, title = "Mrs.", middle_name = ""):
    name = f"{title} {first_name} {middle_name} {last_name}".lstrip()    
    return name

print(full_name("Alice", "Jackson"))

print(full_name("Tom", "Williams", "Dr."))

print(full_name("Michael", "McCarthy", "Mr.", "Steven"))

print(full_name("Brian", "Dilly", "", "Augustus"))

The first two parameters, first_name and last_name, are mandatory, so they must come first. Then title and middle_name are optional and have their default values set. We can call the function with at least the two mandatory arguments. Then we get the default title set to Mrs. and no middle_name (an empty string is the default value). If we call the function with 3 arguments, then title will be the third argument. If we call the function with all four arguments, we’ll get also the middle_name. In the last example we want to pass a middle_name to the function, but no title. The point is that we can’t skip default parameters, that’s why we have to deliver an empty string for the title. Otherwise Augustus would be understood as the title.

Here’s the output:

Mrs. Alice  Jackson
Dr. Tom  Williams
Mr. Michael Steven McCarthy
Brian Augustus Dilly

By the way, the lstrip method on the string is used to get rid of all the whitespaces on the left. You can read more about this function and other functions like that used to strip strings in this article.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply