Chapter 2 Inline Function • To eliminate the cost of call of small function, C++ proposes a new feature called inline function
• An inline function is expanded in a line when the function is called or invoked. i.e. the compiler
replaces the function call with the corresponding function code.
• All inline function must be defined before they are called.
Syntax:
inline return_type function function_name (argument_list)
{
Function body
}
• Some of the situation where inlined expression may not work are;
➢ For function returning value, if a loop ‘switch’ or ‘goto’ statement.
➢ For the function not returning the value if a return statement exist
➢ If the function consists static variable
➢ If the inline functions are recursive
Example program of inline function:
Default Argument • Argument is the data passed in the function. Default argument is useful in situation where some
argument have same value. For e.g. the bank interest for all customers is same.
• Unlike C, C++ allows us to call a function without specifying all its arguments. IN such cases, the
function assigns a default value to the parameter which doesn’t has the matching argument in
the function call.
• Default values are specified when the function is declared.
Example:
float amount(float principal, int period, float r=0.15)
float value = amount(5000,7); //one argument missing. Explicit value of r =0.15
10
Example Program of default argument #include using namespace std;
class weight
{
float w;
public:
void process(float m, float g=9.8)
//here g = 9.8 is used as default argument
{
w= m*g;
cout<<"weight="<}
};
int main()
{
weight ob;
ob.process(10,1.6);
return 0;
}