Introduction to Functions 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 tax amount collected on a purchase is taken from an item’s price; its formula is: 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 Press any key to continue . . .
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: void SetGender(__wchar_t a); double RectangleArea(double L, double W); char Admission(char Category, double Grade, char g); The type of value that a function needs is called a parameter. The actual value that would be processed by the function is called an argument. Supplying the value to a function is referred to as passing the argument. When a function is declared, the argument must be specified by its data type. When the function is called, the argument can be passed using the name of a variable of the same type. You can also pass an actual value to the function instead of a variable name. When a function receives an argument, it can ignore that argument. Here is an example: void Multiply(double Value) { Console::WriteLine(L"Multiplication"); } To call a function that takes an argument, specify the name of the function and its list of arguments (if any) inside of parentheses. You can pass the constant value of an argument. Here is an example: using namespace System; void Multiply(double Value) { Console::WriteLine(L"Multiplication"); } int main() { Multiply(25.75); return 0; } This would produce: Multiplication Press any key to continue . . . When calling the function, if the value is stored in a variable, you can pass the name of the variable in place of the argument. Here is an example: using namespace System; void Multiply(double Value) { Console::WriteLine(L"Multiplication"); } int main() { double HourlySalary = 25.75; Multiply(HourlySalary); return 0; } When defining a function that takes a parameter, in the body of the function, you can use the argument appropriately as you see fit. One the minimum operations you can perform is to involve the argument in an expression. Here is an example: using namespace System; void Multiply(double Value) { double Weekly; Weekly = Value * 36.50; Console::WriteLine(L"Hourly Salary: ${0}", Value); Console::WriteLine(L"Weekly Salary: ${0}", Weekly); } int main() { double HourlySalary = 25.75; Multiply(HourlySalary); return 0; } This would produce: Hourly Salary: $25.75 Weekly Salary: $939.875 Press any key to continue . . . You can also define a function that returns the value of the argument. Here is an example: double Multiply(double Value) { return Value; } Remember that, depending on the goals you set for your function, you can use as many parameters as you want. Here is example: void CalculateRateAmount(double price, double rate) { } As seen in previous sections, the return keyword is used to return an appropriate value from a non-void function. In fact, the item on the right side of the return keyword could be a constant value or a complete expression. The compiler is able to evaluate this item and find out whether it is conform to the return type on the left side of the function name. For this reason, inside of the function that performs a calculation and returns its result, you don't have to first declare a variable that would hold the return value; as long as you provide an appropriate value or expression on the right side of the return keyword, the compiler would be satisfied. Here is example: double CalculateRateAmount(double price, double rate) { return price * rate / 100; } To call a function that takes more than one parameter, provide a value for each parameter in the parentheses of the function, in the order the parameters are listed in the function. When declaring a function, the compiler doesn't require that you supply a name for each argument, it only needs to know the type of argument(s) and the number of arguments a function takes. This information is provided by the presence of a data type. This means you can declare a function with only the data type of an argument. Here is an example: double CalculateRateAmount(double, double); When defining such a function that has been declared as done above, you must provide a name for each argument that you intend to use. The compiler also doesn't care about the name you give an argument when declaring a function. When implementing the function, you just have to remember to use the same name(s) included in the parentheses of the function.
Consider the following program: using namespace System; void Starter(int y) { double a = 112.50; double b = 175.25; a = a / y; b = b + 2; Console::WriteLine("y = {0}", y); Console::WriteLine("a = {0}", a); Console::WriteLine("b = {0}", b); Console::WriteLine("b / a = {0}\n", b / a); } int main() { Starter(2); Starter(2); Starter(2); Starter(2); return 0; } This program would produce: y = 2 a = 56.25 b = 177.25 b / a = 3.15111111111111 y = 2 a = 56.25 b = 177.25 b / a = 3.15111111111111 y = 2 a = 56.25 b = 177.25 b / a = 3.15111111111111 y = 2 a = 56.25 b = 177.25 b / a = 3.15111111111111 Press any key to continue . . . The Starter() function receives one argument passed when it is called. The called function also receives the same argument every time. Looking at the result, the argument passed to the function and the local variables declared inside of the called function keep the same value every time the function is called. That is, when the Starter() function is exited, the values remain the same. We know that, when a function is defined, any variable declared locally belongs to the function and its influence cannot expand beyond the presence of the function. If you want a locally declared variable to keep its changed value when its host function is exited, you can declare such a variable as static. To declare a static variable, type the keyword static on the left of the variable’s data type. For example, if you plan to declare a Radius variable as static in an Area() function, you could write:
You should always initialize a static variable before using it; that is, when declaring it. To make the local variables of our Starter() function static, we can declare them as follows: using namespace System; void Starter(int y) { static double a = 112.50; static double b = 175.25; a = a / y; b = b + 2; Console::WriteLine("y = {0}", y); Console::WriteLine("a = {0}", a); Console::WriteLine("b = {0}", b); Console::WriteLine("b / a = {0}\n", b / a); } int main() { Starter(2); Starter(2); Starter(2); Starter(2); return 0; } This time, when executing the program, it would produce: y = 2 a = 56.25 b = 177.25 b / a = 3.15111111111111 y = 2 a = 28.125 b = 179.25 b / a = 6.37333333333333 y = 2 a = 14.0625 b = 181.25 b / a = 12.8888888888889 y = 2 a = 7.03125 b = 183.25 b / a = 26.0622222222222 Press any key to continue . . . Notice that, this time, each local variable keeps its newly changed value when the function exits. Since a function’s argument can receive different values as the function is called different times, we can test our program by passing different values to its argument as follows: using namespace System; void Starter(int y) { static double a = 112.50; static double b = 175.25; a = a / y; b = b + 2; Console::WriteLine("y = {0}", y); Console::WriteLine("a = {0}", a); Console::WriteLine("b = {0}", b); Console::WriteLine("b / a = {0}\n", b / a); } int main() { Starter(2); Starter(5); Starter(14); Starter(25); Console::WriteLine(); return 0; } The current version of the program would produce: y = 2 a = 56.25 b = 177.25 b / a = 3.15111111111111 y = 5 a = 11.25 b = 179.25 b / a = 15.9333333333333 y = 14 a = 0.803571428571429 b = 181.25 b / a = 225.555555555556 y = 25 a = 0.0321428571428571 b = 183.25 b / a = 5701.11111111111 Press any key to continue . . . |
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|