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
inline return_type function_name(parameter_list) {
// function body
}
Explanation:
inline
: Keyword that suggests to the compiler to insert the function code at each call point.return_type
: Specifies the type of value that the function returns (e.g.,int
,float
).function_name
: The name of the function.parameter_list
: Comma-separated list of parameters the function accepts.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
#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:
#include <stdio.h>
: Includes the standard input/output library for usingprintf()
.static inline int square(int x)
: Declares an inline function that takes an integerx
and returns its square.return x * x;
: Calculates the square ofx
and returns the result.- In the
main()
function, we define an integernum
and callsquare(num)
to get the square ofnum
. - 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
#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:
#include <stdio.h>
: Includes the standard I/O library.static inline int max(int a, int b)
: Declares an inline function that compares two integers,a
andb
.return (a > b) ? a : b;
: Uses the ternary operator to return the greater value betweena
andb
.- In the
main()
function, two integersx
andy
are defined. - The
max(x, y)
function call computes the maximum ofx
andy
, 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:
- The Concept: Inline functions expand the function’s code at each call point.
- Syntax: How to define an inline function using the
inline
keyword. - Examples: Two practical examples that demonstrate inline functions for calculating a square and finding the maximum of two numbers.