0
0
Kotlinprogramming~15 mins

Named arguments for clarity in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Named arguments for clarity
📖 Scenario: You are creating a simple program to calculate the price of a product after applying a discount and adding tax. To make your code clear and easy to read, you will use named arguments when calling functions.
🎯 Goal: Build a Kotlin program that defines a function to calculate the final price of a product using named arguments for clarity. You will create the data, set up a discount rate, use named arguments in the function call, and print the final price.
📋 What You'll Learn
Create a function called calculateFinalPrice with parameters price, discount, and tax.
Use named arguments when calling calculateFinalPrice.
Print the final price with two decimal places.
💡 Why This Matters
🌍 Real World
Using named arguments helps programmers read and understand code easily, especially when functions have many parameters.
💼 Career
Clear code with named arguments is valued in software development jobs because it reduces mistakes and improves teamwork.
Progress0 / 4 steps
1
Create the product price variable
Create a variable called productPrice and set it to 100.0.
Kotlin
Need a hint?

Use val productPrice = 100.0 to create the variable.

2
Set the discount rate
Create a variable called discountRate and set it to 0.15 (which means 15%).
Kotlin
Need a hint?

Use val discountRate = 0.15 to create the discount rate variable.

3
Define the function and use named arguments
Define a function called calculateFinalPrice that takes three Double parameters: price, discount, and tax. It should return the final price after applying the discount and adding tax. Then, create a variable called finalPrice and call calculateFinalPrice using named arguments: price = productPrice, tax = 0.08, and discount = discountRate.
Kotlin
Need a hint?

Define the function with the three parameters and return the final price. Use named arguments in the function call for clarity.

4
Print the final price
Print the value of finalPrice rounded to two decimal places using println. Use "%.2f".format(finalPrice) to format the output.
Kotlin
Need a hint?

Use println("Final price: $" + "%.2f".format(finalPrice)) to print the formatted price.