Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Inline Functions in C++

Inline Functions in C++ | Basics of C++ - Software Development PDF Download

Introduction

In C++, functions are an essential part of programming. They allow programmers to divide the code into smaller, manageable blocks. Inline functions in C++ are a powerful tool that is used to improve the efficiency of code by reducing function call overhead. In this article, we will explain inline functions, how they work, and provide multiple examples to help you understand them better.

What are Inline Functions in C++?

An inline function in C++ is a function that is expanded in line when it is called, rather than executing a call to the function. In other words, the function's code is directly inserted into the calling code by the compiler. Inline functions are faster than regular functions because there is no overhead of function calls.

How to declare an Inline Function in C++?

To declare an inline function in C++, use the 'inline' keyword before the function name and provide the function definition in the same header file where the function is declared. Here's an example of an inline function:

inline int square(int x){

    return x*x;

}

In the example above, the function 'square' is declared as an inline function.

Advantages of Inline Functions:

  • Reduced overhead of function calls, which improves performance.
  • Inline functions reduce code size, as the function code is directly inserted into the calling code.
  • Inline functions can improve the readability of the code by reducing the number of function calls.

Disadvantages of Inline Functions:

  • Inline functions can increase the size of the compiled code, which can affect the performance of the program.
  • Inline functions should be used judiciously, as they can also lead to code duplication and maintenance issues.
  • Inline functions are less flexible than regular functions because they cannot be modified or extended at runtime.

Example 1: Inline Function to Calculate the Area of a Rectangle
Here's an example of an inline function that calculates the area of a rectangle:

#include <iostream>

using namespace std;


inline int rectangle_area(int length, int width){

    return length * width;

}


int main(){

    int length = 5;

    int width = 10;

    cout << "The area of the rectangle is: " << rectangle_area(length, width) << endl;

    return 0;

}

Output

The area of the rectangle is: 50

In the example above, the function 'rectangle_area' is declared as an inline function. The function takes two parameters 'length' and 'width' and returns the area of the rectangle. The 'main' function calls the 'rectangle_area' function to calculate the area of a rectangle.

Example 2: Inline Function to Calculate the Average of Two Numbers
Here's another example of an inline function that calculates the average of two numbers:

#include <iostream>

using namespace std;


inline float average(float a, float b){

    return (a + b) / 2;

}


int main(){

    float num1 = 10.5;

    float num2 = 20.5;

    cout << "The average of " << num1 << " and " << num2 << " is " << average(num1, num2) << endl;

    return 0;

}

Output

The average of 10.5 and 20.5 is 15.5

In the example above, the function 'average' is declared as an inline function. The function takes two parameters 'a' and 'b' and returns their average. The 'main' function calls the average function to calculate the 'average' of two numbers.

Sample Problems:

Write an inline function to calculate the area of a circle. Take the radius of the circle as an input from the user and printSample Problem Solution:
Here's a solution to the sample problem:

#include <iostream>

using namespace std;


inline float circle_area(float radius){

    return 3.14 * radius * radius;

}


int main(){

    float radius;

    cout << "Enter the radius of the circle: ";

    cin >> radius;

    cout << "The area of the circle is: " << circle_area(radius) << endl;

    return 0;

}

Output

Enter the radius of the circle: 5

The area of the circle is: 78.5

In the example above, we have defined an inline function 'circle_area' to calculate the area of a circle. The function takes one parameter 'radius' and returns the area of the circle. The 'main' function takes input from the user for the radius of the circle and calls the 'circle_area' function to calculate the area of the circle.

Conclusion

Inline functions in C++ are a powerful tool that is used to improve the efficiency of code by reducing function call overhead. Inline functions are faster than regular functions because there is no overhead of function calls. They reduce code size and can improve the readability of the code by reducing the number of function calls. However, inline functions should be used judiciously, as they can also lead to code duplication and maintenance issues.

The document Inline Functions in C++ | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Summary

,

Viva Questions

,

Inline Functions in C++ | Basics of C++ - Software Development

,

MCQs

,

Previous Year Questions with Solutions

,

Free

,

study material

,

Inline Functions in C++ | Basics of C++ - Software Development

,

Sample Paper

,

practice quizzes

,

pdf

,

video lectures

,

past year papers

,

mock tests for examination

,

Inline Functions in C++ | Basics of C++ - Software Development

,

Semester Notes

,

Exam

,

Important questions

,

ppt

,

Objective type Questions

,

shortcuts and tricks

,

Extra Questions

;