What is an Inline Function in C?



Inline Function

An inline function in C reduces function calls by expanding the function's code at the call site during compile time. This increases the efficiency of the function. The inline function can be substituted at the point of the function call, but this substitution is always the compiler's choice.

  • In an inline function, the function call is replaced by the actual program code.

  • Most Inline functions are used for small computations and are not suitable for large computing.

  • An inline function is similar to a regular function with the only difference being the use of the inline keyword before the function name.

Syntax

Inline functions are created using the following syntax ?

inline function_name (){
 //function definition
}

Example: Multiplying Two Integers

Following is the C program for the inline function mul to multiply two integers and prints the result of multiplying 2 and 3.

#include <stdio.h>
int mul(int a, int b) { // inline function declaration
    return (a * b);
}

int main() {
    int c;
    c = mul(2, 3);
    printf("Multiplication: %d
", c); return 0; }

Output

When the above program is executed, it produces the following result ?

Multiplication: 6

Example: Adding Two Integers

This C program defines an inline function that is added to sum two integers. If then prints the result of adding 3 and 3, displaying "Addition of 'x' and 'y' is 5".

#include <iostream>
using namespace std;
inline int add(int x, int y) { return x + y; }
int main() {
   cout << "Addition of 'x' and 'y' is: " << add(3, 2) << endl;
   return 0;
}

Output

When the above program is executed, it produces the following result ?

Addition of 'x' and 'y' is: 5
Updated on: 2024-12-13T13:03:49+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements