Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Write a void function definition for a function called add_tax. The function add_tax has two formal parameters: tax_rate, which is the amount of sales tax expressed as a percentage, and cost, which is the cost of an item before tax. The function changes the value of cost so that it includes sales tax.

Short Answer

Expert verified
Question: Define a void function called "add_tax" that takes two parameters: tax_rate and cost. Calculate the cost of an item after including sales tax and update the cost with the new value. Answer: The "add_tax" function is defined as follows: ```cpp void add_tax(double tax_rate, double& cost) { double tax_amount = (cost * tax_rate) / 100; cost = cost + tax_amount; } ``` This function calculates the tax amount by multiplying the cost by the tax_rate and dividing by 100. The cost is then updated by adding the tax_amount to the original cost.

Step by step solution

01

Understand the void function and parameters

A void function does not return a value and its result is directly reflected on the input parameters when working with reference types or pointer types, like in this case with the cost variable. The formal parameters are tax_rate and cost. Tax_rate is the sales tax percentage, and cost is the price of the item before tax.
02

Calculate the tax amount

To calculate the tax amount, we need to multiply the cost by the tax_rate and then divide by 100 to convert percentage to a decimal form. The formula is: tax_amount = (cost * tax_rate) / 100
03

Update the cost to include tax

Once the tax amount is calculated, we need to update the cost variable to include the tax. We can do this by simply adding the tax_amount to the current cost. The formula is: cost = cost + tax_amount Now, let's put it all together in the function definition: ```cpp void add_tax(double tax_rate, double& cost) { double tax_amount = (cost * tax_rate) / 100; cost = cost + tax_amount; } ``` Note that we use double& for cost parameter so that it's updated within the function and reflects the changes when the function finishes execution.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free