0% found this document useful (0 votes)
3 views3 pages

Inline

Uploaded by

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

Inline

Uploaded by

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

The compiler may reject the inlining request.

The compiler may not


implement inlining in situations like these:

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


2. if a function has static variables.
3. Whether a function recurses.
4. If the return statement is absent from the function body and the
return type of the function is not void.
5. Whether a function uses a goto or switch statement.

Syntax for an inline function:

1. inline return_type function_name(parameters)


2. {
3. // function code?
4. }

Let's understand the difference between the normal function and


the inline function.

Inside the main() method, when the function fun1() is called, the control
is transferred to the definition of the called function. The addresses from
where the function is called and the definition of the function are different.
This control transfer takes a lot of time and increases the overhead.

When the inline function is encountered, then the definition of the function
is copied to it. In this case, there is no control transfer which saves a lot of
time and also decreases the overhead.

Let's understand through an example.


1. #include <iostream>
2. using namespace std;
3. inline int add(int a, int b)
4. {
5. return(a+b);
6. }
7. int main()
8. {
9. cout<<"Addition of 'a' and 'b' is:"<<add(2,3);
10. return 0;
11.
12. }

Once the compilation is done, the code would be like as shown as below:

1. #include<iostream>
2. using namespace std;
3. inline int add(int a, int b)
4. {
5. return(a+b);
6. }
7. int main()
8. {
9. cout<<"Addition of 'a' and 'b' is:"<<return(2+3);
10. return 0;
11. }

You might also like