Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
8.1 Mб
Скачать

Int main()

{

CreateAndShowProperty();

Console::WriteLine();

return 0;

}

2. Execute the application to test it  

3. Close the DOS window

9.3Introduction to Functions Parameters

9.3.1 Overview of Parameters

Imagine you want to write a program that calculates an item’s purchase price based on the item’s store price added the tax. The tax rate is a percentage value. This means a tax rate set at 7.50% in C++ terms is equal to 0.075 (because 7.50/100 = 0.075).

The formula of calculating the final price of an item is:

Final Price = Item Price + Tax Amount

Here is an example:

using namespace System;

double RequestOriginalPrice()

{

double Price;

Console::Write("Enter the original price: $");

Price = double::Parse(Console::ReadLine());

return Price;

}

double RequestDiscountRate()

{

double Discount;

Console::Write("Enter discount rate(0.00 to 100.00): ");

Discount = double::Parse(Console::ReadLine());

return Discount;

}

double RequestTaxRate()

{

double Tax;

Console::Write("Enter the tax rate(0.00 to 100.00): ");

Tax = double::Parse(Console::ReadLine());

return Tax;

}

Int main()

{

double OriginalPrice, DiscountRate, PriceAfterDiscount, TaxRate;

double DiscountAmount, TaxAmount, NetPrice;

OriginalPrice = RequestOriginalPrice();

DiscountRate = RequestDiscountRate();

TaxRate = RequestTaxRate();

DiscountAmount = OriginalPrice * DiscountRate / 100;

PriceAfterDiscount = OriginalPrice - DiscountAmount;

TaxAmount = PriceAfterDiscount * TaxRate / 100;

NetPrice = PriceAfterDiscount + TaxAmount;

Console::WriteLine("\nReceipt");

Console::WriteLine("Original Price: {0:C}", OriginalPrice);

Console::WriteLine("Discount Rate: {0}%", DiscountRate);

Console::WriteLine("Discount Amount: {0:C}", DiscountAmount);

Console::WriteLine("After Discount: {0:C}",

PriceAfterDiscount);

Console::WriteLine("Tax Rate: {0}%", TaxRate);

Console::WriteLine("Tax Amount: {0:C}", TaxAmount);

Console::WriteLine("Net Price: {0:C}", NetPrice);

return 0;

}

Here is an example of running the program:

Enter the original price: $450

Enter discount rate(0.00 to 100.00): 70

Enter the tax rate(0.00 to 100.00): 5.75

Receipt

Original Price: $450.00

Discount Rate: 70%

Discount Amount: $315.00

After Discount: $135.00

Tax Rate: 5.75%

Tax Amount: $7.76

Net Price: $142.76

9.3.2Arguments to a Function

Some functions will need to receive a value in order to perform their assignment; some others will not. Some function will have many needs and some others will not. A function’s need is called a parameter. If a function has a lot of such needs, these are its parameters. A parameter is the type of the variable that the function would need to perform its assignment. The most important aspect of a parameter is its data type. This data type is required when the function is declared. When the function is defined, you must specify both the data type and the name of the parameter.

When declaring a function that use one or more parameters, specify each parameter with a data type and an optional name. Here are examples of declaring functions that take parameters: