Inline Functions

C Inline Functions allow you to suggest that the compiler insert the function’s code at each point of call instead of performing a traditional function call, potentially improving performance for small, frequently used functions.


What are Inline Functions?

Inline functions are defined with the inline keyword. They provide a way to avoid the overhead of function calls by expanding the function’s code in place. Although the compiler may choose not to inline a function, using this keyword is a useful hint when working with small functions.

Syntax of Inline Functions

</>
Copy
inline return_type function_name(parameter_list) {
    // function body
}

Explanation:

  1. inline: Keyword that suggests to the compiler to insert the function code at each call point.
  2. return_type: Specifies the type of value that the function returns (e.g., int, float).
  3. function_name: The name of the function.
  4. parameter_list: Comma-separated list of parameters the function accepts.
  5. function body: Contains the code that gets executed when the function is called.

Example 1: Inline Function to Calculate the Square of a Number

In this example, we create an inline function called square that calculates and returns the square of an integer.

main.c

</>
Copy
#include <stdio.h>

static inline int square(int x) {
    return x * x;
}

int main() {
    int num = 5;
    printf("Square of %d is %d\n", num, square(num));
    return 0;
}

Explanation:

  1. #include <stdio.h>: Includes the standard input/output library for using printf().
  2. static inline int square(int x): Declares an inline function that takes an integer x and returns its square.
  3. return x * x;: Calculates the square of x and returns the result.
  4. In the main() function, we define an integer num and call square(num) to get the square of num.
  5. The printf() function outputs the result to the console.

Output:

Square of 5 is 25

Example 2: Inline Function to Find the Maximum of Two Numbers

In this example, we define an inline function called max that returns the greater of two integers using the ternary operator.

main.c

</>
Copy
#include <stdio.h>

static inline int max(int a, int b) {
    return (a > b) ? a : b;
}

int main() {
    int x = 10, y = 20;
    printf("Maximum of %d and %d is %d\n", x, y, max(x, y));
    return 0;
}

Explanation:

  1. #include <stdio.h>: Includes the standard I/O library.
  2. static inline int max(int a, int b): Declares an inline function that compares two integers, a and b.
  3. return (a > b) ? a : b;: Uses the ternary operator to return the greater value between a and b.
  4. In the main() function, two integers x and y are defined.
  5. The max(x, y) function call computes the maximum of x and y, which is then printed to the console.

Output:

Maximum of 10 and 20 is 20

Conclusion

In this tutorial, we explored C inline functions and learned how they can be used to reduce function call overhead. We covered:

  1. The Concept: Inline functions expand the function’s code at each call point.
  2. Syntax: How to define an inline function using the inline keyword.
  3. Examples: Two practical examples that demonstrate inline functions for calculating a square and finding the maximum of two numbers.