AIM: Write a C++ program for inline functions with default arguments to calculate simple interest
C++ provides an inline functions to reduce the function call overhead. Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time. Inline function may increase efficiency if it is small.
The syntax for defining the function inline is:
inline return-type function-name(parameters)
// function code
Remember, inlining is only a request to the compiler, not a command. Compiler can ignore the request for inlining. Compiler may not perform inlining in such circumstances like:
- If a function contains a loop. (for, while, do-while).
- If a function contains static variables.
- If a function is recursive.
- If a function return type is other than void, and the return statement doesn’t exist in function body.
- If a function contains switch or goto statement.
Inline functions provide following advantages:
- Function call overhead doesn’t occur.
- It also saves the overhead of push/pop variables on the stack when function is called.
- It also saves overhead of a return call from a function.
- When you inline a function, you may enable compiler to perform context specific optimization on the body of function. Such optimizations are not possible for normal function calls. Other optimizations can be obtained by considering the flows of calling context and the called context.
- Inline function may be useful (if it is small) for embedded systems because inline can yield less code than the function call preamble and return.
Inline function disadvantages:
- The added variables from the inlined function consumes additional registers, After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization. This means that when inline function body is substituted at the point of function call, total number of variables used by the function also gets inserted. So the number of register going to be used for the variables will also get increased. So if after function inlining variable numbers increase drastically then it would surely cause an overhead on register utilization.
- If you use too many inline functions then the size of the binary executable file will be large, because of the duplication of same code.
using namespace std;
inline float intrest(float p,float t=it(),float r=ir());
inline float intrest(float p,float t,float r)
cout<<“\nEnter rate of interest: “;
cout<<“Enter number of months: “;
cout<<“simple interest without specific argument: “<<intrest(1000,2,2);
cout<<“\n simple interest called default argument: “<<intrest(1000);
simple interest without specific argument: 40
Enter rate of interest: 3
Enter number of months: 6
simple interest called default argument: 180