0% found this document useful (0 votes)
14 views

inline fuctions

Online

Uploaded by

jaijaigs05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

inline fuctions

Online

Uploaded by

jaijaigs05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 8 Function Overloading

Inline functions:
Normal function usually has an overhead i.e (the argument passing & the value returning
activities with a function call) during the execution time of a program. We use inline functions to
avoid these overheads.

Definition:
A function whose body/code is inserted at the place of its function call. They are short
functions.

Inline function is a function that replaces the function call by body of function. An inline function is a
function that is expanded in-line when it is invoked.

Characteristics of Inline Functions:


• They are defined with the keyword “inline“.
• It should be defined before all functions call it.
• The compiler replaces the function call statement with the function code itself and then compiles
the entire code.
• They run little faster than normal functions.
• They are generally used for frequently used function(repeated code)
• These functions may or may not be the member function of a class.

Syntax:
inline return-type-specifier function_name (arguments)
{
statements;
return expression;
}

Example 1: Example 2:
inline int cube(int a) inline int max(int a,int b)
{ {
return (a*a*a); return (a>b? a : b);
} }

Advantages of Inline Function:


1. The inline member functions are compact function calls.
2. Therefore the size of the object code is considerably reduced.
3. The speed of execution of a program increases.
4. Very efficient code can be generated.
5. There is no burden on the system for function call.
6. It also saves the overhead of returning a value to a function.

Disadvantages of Inline Function:


1. As the body of inline function is replaced in place of a function call, the size of the executable file
also increases.
2. More memory is needed.
3. No conditional statements (if, if-else, nested-if, switch), loops (for, while, do-while) or any other
branching statements are allowed.
4. Recursion is not possible.
5. Inline functions should be small.

St. Joseph’s PU College Page 1


Chapter 8 Function Overloading

Limitations of Inline Function:

NOTE: The inline function may not work some time for one of the following reasons (limitations)

 If inline function is too long or too complicated.


 If inline function is recursive.
 If inline function has looping construct.
 If inline function has a switch or goto statements.

Write a C++ Program to find the cube of a number using inline functions

#include <iostream.h>
#include <conio.h>

class line
{
public: inline int cube (int s)
{
return(s*s*s);
}
};

void main()
{
line I;
int x,y;
clrscr();
cout<<“Enter a number”;
cin>>x;
y=I.cube(x);
cout<<“the cube of“<<x<<“=“<<y;
getch();
}

~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

St. Joseph’s PU College Page 2

You might also like