Skip to content

Exercise 7.1: Optional parameters

Related course:

Function parameters

Function parameters can be optional. This means that when calling a function, you have the option to set the variable or not.

A function with an optional parameter may be called in multiple ways:

addNumbers(2,1)
addNumbers(2,1,4)

To make a variable optional, add =None to the parameter.

Type the program shown below and run it:

def addNumbers(a,b, c=None):

    if c is not None:
        return a+b+c
    else:
        return a+b


print( addNumbers(2,3) )
print( addNumbers(2,3,4) )

Study drill

Try the exercises below

1. Create a function that can be called in these ways:

person("Abraham")
person("Abraham","Lincoln")

After completing these continue with the next exercise.

Answers

Functions
Lists
Published inUncategorized

Be First to Comment

    Leave a Reply