Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Modifying Functions | Functions
Introduction to Python

bookModifying Functions

Functions are flexible tools in programming. You can change and improve them to fit different needs. In this chapter, you will learn how to make functions more useful by using default arguments and keyword arguments.

Let's look at a simple grocery store example to show how you can modify functions to be more helpful:

Default Arguments

Default arguments are a useful feature in Python that allow you to specify default values for function parameters.

In the apply_discount() function, the discount parameter is set to 0.10 by default. This means the function will automatically apply a 10% discount unless otherwise specified. As seen in the default_discount_price variable, we can call the function with just the price parameter.

However, if needed, we can override the default value by passing both the price and a custom discount (e.g., 0.20 for 20%) as demonstrated with the custom_discount_price variable.

123456789101112
# Define a function with a default `discount` argument def apply_discount(price, discount=0.10): discounted_price = price * (1 - discount) return discounted_price # Call the function without providing a `discount`, using the default value default_discount_price = apply_discount(100) print(f"Price after applying the default discount: ${default_discount_price}") # Call the function with a custom `discount` value custom_discount_price = apply_discount(100, 0.20) print(f"Price after applying a custom discount: ${custom_discount_price}")
copy

Keyword Arguments

Keyword arguments in Python allow you to pass arguments by explicitly naming each parameter, making your function calls more readable and flexible. This is particularly helpful when a function has multiple parameters or when the order of arguments could be confusing.

In the following example, both price and discount are specified, while the tax parameter remains at its default value, providing flexibility without compromising clarity.

12345678
# Function where `tax` has a default value def calculate_total(price, discount, tax=0.05): total = price * (1 + tax) * (1 - discount) return total # Calling the function using keyword arguments total_cost = calculate_total(price=100, discount=0.15) print(f"Total cost after applying discount: ${total_cost}")
copy
Note
Note

The order of parameters doesn't matter when they are passed using keyword arguments.

Task

Swipe to start coding

Create a simple checkout system for a grocery store item using default values and keyword arguments.

  • Define apply_discount(price, discount=0.10) β†’ Returns the price after discount (10% by default).
  • Define add_tax(price, tax=0.05) β†’ Returns the price after tax is added (5% by default).
  • Define final_price(price, discount=0.10, tax=0.05) β†’ Uses apply_discount() first, then add_tax(), and returns the final result.

Then run two examples:

  • Call final_price(50) using the defaults.
  • Call final_price(50, tax=0.08) using a custom tax rate via a keyword argument.

Output Requirements

Print exactly these two lines (rounded to 2 decimals):

  • Final price with default discount and tax: $<value>
  • Final price with custom tax: $<value>

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 6
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookModifying Functions

Swipe to show menu

Functions are flexible tools in programming. You can change and improve them to fit different needs. In this chapter, you will learn how to make functions more useful by using default arguments and keyword arguments.

Let's look at a simple grocery store example to show how you can modify functions to be more helpful:

Default Arguments

Default arguments are a useful feature in Python that allow you to specify default values for function parameters.

In the apply_discount() function, the discount parameter is set to 0.10 by default. This means the function will automatically apply a 10% discount unless otherwise specified. As seen in the default_discount_price variable, we can call the function with just the price parameter.

However, if needed, we can override the default value by passing both the price and a custom discount (e.g., 0.20 for 20%) as demonstrated with the custom_discount_price variable.

123456789101112
# Define a function with a default `discount` argument def apply_discount(price, discount=0.10): discounted_price = price * (1 - discount) return discounted_price # Call the function without providing a `discount`, using the default value default_discount_price = apply_discount(100) print(f"Price after applying the default discount: ${default_discount_price}") # Call the function with a custom `discount` value custom_discount_price = apply_discount(100, 0.20) print(f"Price after applying a custom discount: ${custom_discount_price}")
copy

Keyword Arguments

Keyword arguments in Python allow you to pass arguments by explicitly naming each parameter, making your function calls more readable and flexible. This is particularly helpful when a function has multiple parameters or when the order of arguments could be confusing.

In the following example, both price and discount are specified, while the tax parameter remains at its default value, providing flexibility without compromising clarity.

12345678
# Function where `tax` has a default value def calculate_total(price, discount, tax=0.05): total = price * (1 + tax) * (1 - discount) return total # Calling the function using keyword arguments total_cost = calculate_total(price=100, discount=0.15) print(f"Total cost after applying discount: ${total_cost}")
copy
Note
Note

The order of parameters doesn't matter when they are passed using keyword arguments.

Task

Swipe to start coding

Create a simple checkout system for a grocery store item using default values and keyword arguments.

  • Define apply_discount(price, discount=0.10) β†’ Returns the price after discount (10% by default).
  • Define add_tax(price, tax=0.05) β†’ Returns the price after tax is added (5% by default).
  • Define final_price(price, discount=0.10, tax=0.05) β†’ Uses apply_discount() first, then add_tax(), and returns the final result.

Then run two examples:

  • Call final_price(50) using the defaults.
  • Call final_price(50, tax=0.08) using a custom tax rate via a keyword argument.

Output Requirements

Print exactly these two lines (rounded to 2 decimals):

  • Final price with default discount and tax: $<value>
  • Final price with custom tax: $<value>

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 6
single

single

some-alt