An **Inline Function** is a function that the compiler attempts to expand at the point of call, rather than performing a traditional function call. Instead of jumping to a separate section of memory to execute the function and then jumping back, the compiler simply replaces the function call with the actual code of the function.
Every time a standard function is called, the CPU performs several steps: saving registers, pushing arguments onto the stack, and jumping to the function address. For very small functions, this "overhead" can actually take longer than executing the code inside the function itself.
To suggest inlining to the compiler, place the inline keyword before the return type in the function definition.
Before inline was standardized, programmers used #define macros to save time. However, inline functions are much safer and professional.
| Feature | Inline Function | Macro (#define) |
|---|---|---|
| Type Checking | Yes (Safe) | No (Risky) |
| Debugging | Easier (Step-through) | Difficult (Text replace) |
| Logic Handling | Behaves like a real function. | Can have unexpected side effects. |
It is important to remember that inline is a **request**, not a command. The compiler may ignore it if:
static variables.In professional C development, you will often see static inline used in header files. This ensures that each source file (.c) that includes the header gets its own local version of the function, preventing "multiple definition" errors during the linking phase.