Exploring Functions |
|
When a function receives an argument, it performs one
of two actions with regards to the value of the argument; it might modify
the value itself or only use the argument to modify another argument or
another of its own variables. If you know that the function is not
supposed to alter the value of an argument, you should let the compiler
know. This is a safeguard that serves at least two purposes. First, the
compiler will make sure that the argument supplied stays intact; if the
function tries to modify the argument, the compiler would throw an error,
letting you know that an undesired operation took place. Second, this
speeds up execution. #include <iostream> using namespace std; float Perimeter(float l, float w) { double p; p = 2 * (l + w); return p; } int main() { float length, width; cout << "Rectangle dimensions.\n"; cout << "Enter the length: "; cin >> length; cout << "Enter the width: "; cin >> width; cout << "\nThe perimeter of the rectangle is: " << Perimeter(length, width) << "\n\n"; return 0; } This would produce: Rectangle dimensions. Enter the length: 35.55 Enter the width: 28.75 The perimeter of the rectangle is: 2044.12 As you can see, the Perimeter() function does not change the values of the length or the width. To reinforce the purpose of the assignment, you should make this clear to the compiler. To make the length and the width arguments constant, you would change the declaration of the Perimeter() function as follows: float Perimeter(const float l, const float w); You can make just one or more arguments constants, and there is no order on which arguments can be made constant.
If you pass an argument as reference, the compiler
would access the argument from its location. The called function can
modify the value of the argument. The advantage is that the code execution
is faster because the argument gives access to its address. The
disadvantage could be that if the calling function modifies the value of
the argument, when the function exits, the value of the argument would
have (permanently) changed and the original value would be lost (actually,
this can be an advantage as we have learned in the passed). If you do not
want the value of the passed argument to be modified, you should pass the
argument as a constant reference. When doing this, the compiler would
access the argument at its location (or address) but it would make sure
that the value of the argument stays intact. double CalculateNetPrice(const double& tax) { double original; const double discount = 25; Original = GetOriginalPrice(); double discountValue = original * discount / 100; double taxValue = tax / 100; double netPrice = original - discountValue + taxValue; return NetPrice; } You can mix arguments passed by value, those passed as reference, those passed by constant, and those passed by constant references. You will decide, based on your intentions, to apply whatever technique suits your scenario. The following program illustrates the use of various techniques of passing arguments: #include <iostream> using namespace std; // Passing an argument by reference void GetOriginalPrice(double& originalPrice) { cout << "Enter the original price of the item: $"; cin >> originalPrice; } // Passing an argument as a constant reference // Passing arguments by value double CalculateNetPrice(const double& original, double tax, double discount) { discount = original * discount / 100; tax = tax / 100; double netPrice = original - discount + tax; return netPrice; } int main() { double taxRate = 5.50; // = 5.50% const double tiscount = 25; double price; double original; void Receipt(const double& orig, const double& taxation, const double& dis, const double& final); GetOriginalPrice(original); price = CalculateNetPrice(original, taxRate, discount); Receipt(original, taxRate, discount, price); cout << "\n\n"; return 0; } void Receipt(const double& original, const double& tax, const double& discount, const double& finalPrice) { cout << "\nReceipt"; cout << "\nOriginal Price: $" << original; cout << "\nTax Rate: " << tax << "%"; cout << "\nDiscount Rate: " << discount << "%"; cout << "\nFinal Price: $" << finalPrice; }
|
|
Functions and Namespaces |
Functions Local Definition |
Like a variable, a function can be part of a namespace. To declare a function in a namespace, provide its return type, followed by a name, followed by the argument(s), if any, inside of parentheses. Here is an example: |
namespace InterestAndDiscount { double Principal; double Rate; int Time; double CalculateDiscount(); double CalculateInterest(); double CalculateMaturity(); }
A member function of a namespace can be accessed using the scope access operator. There are two main ways you can implement a member function. In the body of the namespace, which is a local implementation, delimit the body of the function with an opening curly bracket “{“ and a closing curly bracket “}”. A function that is a member of a namespace has complete access to the member variables of the same namespace. Therefore, you do not have to pass the member variables as arguments to the member functions. Here is an example: |
#include <iostream> using namespace std; namespace InterestAndDiscount { double Principal; double Rate; int Time; double GetInterestRate() { return Rate / 100; } double CalculateInterest() { return Principal * GetInterestRate() * Time; } double CalculateMaturity() { return Principal + CalculateInterest(); } } int main() { using namespace InterestAndDiscount; Principal = 12500; // $ Rate = 12.25; // % Time = 4; // Years cout << "Interest Calculation"; cout << "\nPrincipal: $" << Principal << "\nRate: " << Rate << "%" << "\nTime: " << Time << " years" << "\nInterest: $" << CalculateInterest() << "\nMaturity: $" << CalculateMaturity() << "\n\n"; return 0; } This would produce: Interest Calculation Principal: $12500 Rate: 12.25% Time: 4 years Interest: $6125 Maturity: $18625
If a nested namespace has its own functions, you can also implement them in the body of the nested namespace. Here is an example: |
namespace InterestAndDiscount { double Principal; double Rate; int Time; double GetInterestRate() { return Rate / 100; } double CalculateInterest() { return Principal * GetInterestRate() * Time; } double CalculateMaturity() { return Principal + CalculateInterest(); } namespace Discounter { double Maturity; double DiscountRate; double TermOfDiscount; double Discount() { return Maturity * DiscountRate * TermOfDiscount; } } }
After locally implementing the member functions of a nested namespace, you can access its members and display their value in the main() function as done above. |
Functions Global Definitions |
To implement a member function outside the body of a namespace, provide the return type, followed by the name of the namespace, followed by the scope access operator “::”. Here is an example: |
namespace InterestAndDiscount { double Principal; double Rate; int Time; double GetInterestRate(); double CalculateInterest(); double CalculateMaturity(); } InterestAndDiscount::GetInterestRate() { return Rate / 100; } InterestAndDiscount::CalculateInterest() { return Principal * GetInterestRate() * Time; } InterestAndDiscount::CalculateMaturity() { return Principal + CalculateInterest(); }
To implement the member functions of a nested namespace outside of the parent namespace, you must qualify each member function to specify the function (or the variable) you are calling. Here is an example: |
namespace InterestAndDiscount { double Principal; double Rate; int Time; double GetInterestRate(); double CalculateInterest(); double CalculateMaturity() namespace Discounter { double Maturity; double DiscountRate; double TermOfDiscount; double Discount(); } } . . . InterestAndDiscount::Discounter::Discount() { return Maturity * DiscountRate * TermOfDiscount; }
Namespaces and External Functions |
The member variables of a namespace are variables like any of those we have used so far. They can request their values from an outside function. Here is an example: |
#include <iostream> using namespace std; namespace InterestAndDiscount { . . . } . . . int main() { using namespace InterestAndDiscount; double GetThePrincipal(); cout << "Loan Processing\n"; cout << "Enter the following values\n"; Principal = GetThePrincipal(); cout << "Rate (between 0 and 100): "; cin >> Rate; cout << "Time (number of years): "; cin >> Time; cout << "\nInterest on a loan"; cout << "\nPrincipal: $" << Principal; cout << "\nRate: " << Rate << "%"; cout << "\nTime: " << Time << " years"; cout << "\nInterest: $" << CalcInterest(); cout << "\nMaturity Value: $" << CalcMaturityValue(); return 0; } double GetThePrincipal() { double P; cout << "Principal: $"; cin >> P; return P; }
The member variable of a namespace can also be passed as argument to a function. When passing the argument, if the using namespace routine has been entered, you can pass the argument like any other. Otherwise, you should qualify the namespace member with the :: operator. In the following example, one member of a namespace is passed by its name only because of the previous using namespace. The other members are passed by being qualified, which is for demonstration purposes only: |
#include <iostream> using namespace std; namespace InterestAndDiscount { . . . } . . . int main() { using namespace InterestAndDiscount; void GetThePrincipal(double& p); void RateAndTime(double &r, double &t); cout << "Loan Processing"; cout << "\nEnter the following values\n"; GetThePrincipal(Principal); RateAndTime(InterestAndDiscount::Rate, InterestAndDiscount::Time); cout << setiosflags(ios::fixed) << setprecision(2); cout << "\nInterest on a loan"; cout << "\nPrincipal: $" << Principal; cout << "\nRate: " << Rate << "%"; cout << setiosflags(ios::fixed) << setprecision(0); cout << "\nTime: " << Time << " years"; cout << setiosflags(ios::fixed) << setprecision(2); cout << "\nInterest: $" << CalcInterest(); cout << "\nMaturity Value: $" << CalcMaturityValue(); return 0; } void GetThePrincipal(double& P) { cout << "Principal: $"; cin >> P; while( P < 0 ) { cout << "Enter a positive number: $"; cin >> P; } } void RateAndTime(double &rate, double &time) { do { cout << "Rate (between 0 and 100): "; cin >> rate; } while(rate < 0 || rate > 100); do { cout << "Time (Nbr of Years): "; cin >> time; } while(time <= 0 || time >= 30); }
C++ Built-in Functions |
Introduction |
Although as a smart programmer you can create any function to perform a desired job, the C++ language provides a series of functions already made so you can just add them to your program without caring how they work, all you need to know is what these functions do. The functions that are part of the C++ language are highly valuable, were tested sufficiently, and are completely reliable. The C++ built-in functions are made for various assignments ranging from algebra, geometry, trigonometry, and finance, etc. Besides the functions that are part of the C++ Standard, each compiler ships with its own set of functions that may not be available on other compilers. Borland C++ Builder provides an extremely rich library of functions. |
Asserting a Value or an Expression |
Most of the values you use in your program will need to fit in an interval of your choice. For example, when requesting the age of a person, you would need such a value to be positive. After all, you do not expect a person to be –26 years old. C++ provides a function that can be used to check that a value or expression responds to a criteria of your choice. This function is called assert() and is defined in the cassert library of the std namespace. Its syntax is: |
void assert(int Expression);
The assert() function considers an expression as its argument and tests it. This function is used in the same context as the conditional statements we will study in the next lesson of this book. If the Expression is true, assert() acknowledges that and lets the compiler continue with the next operation. If the Expression is false, assert() displays a (nasty) message. Although we have not yet learned conditional statements, the following is an example that requests the age of a student and checks that the supplied age is valid only if the student is older than 8: |
#include <iostream> #include <cassert> using namespace std; int main() { float StudentAge; cout << "Type Student's Age: "; cin >> StudentAge; assert(StudentAge > 8); cout << "Student Age: " << StudentAge << "\n\n"; return 0; }
Mathematic Functions |
The C++ language also provides a series of functions
to perform various mathematically related operations. The functions are
defined in various libraries and this can depend on the compiler you are
using.
Additional functions are defined in the cstdlib library and they are:
|
|
||
Previous | Copyright © 1998-2015 FunctionX, Inc. | Next |
|