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

inline functions lect 15

Inline functions in C++ reduce execution time by replacing function calls with the function's code, similar to macros in C. They are beneficial for small functions called multiple times, saving time on control transfer, but the compiler may ignore inlining requests under certain conditions. The document outlines the syntax for defining inline functions, their advantages, and provides examples of their usage.

Uploaded by

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

inline functions lect 15

Inline functions in C++ reduce execution time by replacing function calls with the function's code, similar to macros in C. They are beneficial for small functions called multiple times, saving time on control transfer, but the compiler may ignore inlining requests under certain conditions. The document outlines the syntax for defining inline functions, their advantages, and provides examples of their usage.

Uploaded by

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

Btech – Cse III Sem Inline Functions

UNIT-2 (Inline Functions)


LECTURE NO: -15
Inline Functions
An inline function in C++ is feature that reduces the execution time of a program, just as we
have macros in C. This concept of inline functions is used while handling Classes in C++.
Whenever such a function is encountered, the compiler replaces it with a copy of that
function’s code (function definition). In other words, one may say that such a function is
expanded in-line, hence named as an inline function.

Inline functions are commonly used when the function definitions are small, and the
functions are called several times in a program. Using inline functions saves time to transfer
the control of the program from the calling function to the definition of the called function.

To inline a function, place the keyword inline before the function name and define the
function before any calls are made to the function. The compiler can ignore the inline
qualifier in case defined function is more than a line.

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:-

1) If a function contains a loop. (for, while, do-while)

2) If a function contains static variables.

3) If a function is recursive.

4) If a function contains switch or goto statement.

1
Notes By: Shirish Mohan Dubey (Asst. Prof. CS Dept.)
Btech – Cse III Sem Inline Functions

Following is an example, which makes use of inline function to return max of two numbers.

#include <iostream.h>

inline int add(int a, int b)

return(a+b);

Void main()

cout<<"Addition of 'a' and 'b' is:"<<add(2,3); or int x,y;

or cin>>x,y;

or cout<<”Addition of a and b is “<<add(x,y);

getch();

Output:-

Ans-5

Exampole:-

#include <iostream.h>

inline int Max(int x, int y)

return (x > y)? x : y;

// Main function for the program

Void main () {

cout << "Max (20,10): " << Max(20,10) << endl;

cout << "Max (0,200): " << Max(0,200) << endl;

cout << "Max (100,1010): " << Max(100,1010) << endl;

2
Notes By: Shirish Mohan Dubey (Asst. Prof. CS Dept.)
Btech – Cse III Sem Inline Functions

getch();

Output: -

Max (20,10): 20

Max (0,200): 200

Max (100,1010): 1010

Why do we need an inline function in C++?

The main use of the inline function in C++ is to save memory space. Whenever the function
is called, then it takes a lot of time to execute the tasks, such as moving to the calling
function. If the length of the function is small, then the substantial amount of execution
time is spent in such overheads, and sometimes time taken required for moving to the
calling function will be greater than the time taken required to execute that function.

The solution to this problem is to use macro definitions known as macros. The pre-
processor macros are widely used in C, but the major drawback with the macros is that
these are not normal functions which means the error checking process will not be done
during the compilation.

C++ has provided one solution to this problem. In the case of function calling, the time for
calling such small functions is huge, so to overcome such a problem, a new concept was
introduced known as an inline function.

Advantages of inline function

 In the inline function, we do not need to call a function, so it does not cause any
overhead.

 It also saves the overhead of the return statement from a function.

 It does not require any stack on which we can push or pop the variables as it does
not perform any function calling.

3
Notes By: Shirish Mohan Dubey (Asst. Prof. CS Dept.)

You might also like