Welcome to the Using Default Arguments Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

Learning Outcomes

In this module, you will complete the following exercise:

  • Exercise 1 - Defining Functions with Default Arguments

After completing this lab, you will be able to:

  • Define functions with default arguments in Python programs

Exam Objectives

The following exam objectives are covered in this lab:

  • 4.1 Construct and analyze code segments that include function definitions

Lab Duration

It will take approximately 10 minutes to complete this lab.

Exercise 1 - Defining Functions with Default Arguments

In Python, if a function is defined with two arguments, you will need to supply the required arguments while calling the function, failing which the interpreter will raise an error.

For example, recall the addition function defined in the Calculator.py program.

This function adds two numbers and prints the result

def add(x, y):

temp = x + y

print(“The sum of the two numbers, {} + {} is: {}”.format (x, y, temp))

The above function expects two numbers to be supplied to it on each function call. A typical call of this function will be as follows:

add(7, 10):

The interpreter will raise an error if both or either of the arguments is missed.

However, if you set default values for these arguments, you can trick the interpreter into believing that the arguments were supplied.

To do that, you can modify the function definition as follows:

This function adds two numbers and prints the result

def add(x=0, y=0):

temp = x + y

print(“The sum of the two numbers, {} + {} is: {}”.format (x, y, temp))

This function, if called without arguments, will assume the default values of “0” for both numbers and print “0” as the answer.

Please note that you have the flexibility of setting default values for one or more arguments, as desired. All the arguments with default values can be skipped during the function calls, while the others remain mandatory. For example, if you defined the add function header as:

def add(x, y=0):

While calling this function, you may skip passing the y argument because that has a default value defined for it. However, if you skip the x value, you will encounter a compilation error.

It is important to note that any number of arguments in a function can have a default value. But once you have a default argument, all the arguments to its right must also have default values.

This means that arguments with no default value cannot follow the arguments with a default value. For example, if you had defined the function header above as:

def add(x=0, y):

You will get an error.

In this exercise, you will write a program to define a simple function that accepts both default and non-default arguments. The program will call this function with or without arguments.

Learning Outcomes

After completing this exercise, you will be able to:

  • Accept console inputs and print formatted output in Python programs

Comprehensive Learning

See the full benefits of our immersive learning experience with interactive courses and guided career paths.