Content
Hello everyone, I am Bug Jun~For C developers, functions are definitely not unfamiliar, and you could even type them out with your eyes closed. But have you categorized the functions in C language? When you write a function, do you first think about its functionality or its parameters?Today, I suggest categorizing the functions you write so that you can know how to use them faster in the future. Here, I will simply divide them into three categories:
1
Functions that only depend on parameters for their return value
These functionsdo not read or modify any global variables, static variables, or memory accessed indirectly through pointers (including the <span><span>this</span></span> pointer). These functionshave no observable side effects (other than returning a value). They cannot modify any external state, perform I/O operations, or call other functions that may have side effects. A typical example is these pure mathematical functions, whose output is entirely determined by their input and does not involve any external state.
int square(int x)
{
return x * x;
}
double circle_area(double radius)
{
return 3.14159 * radius * radius;
}
Therefore, we can safelycache (memoize) the results of these function calls. If the function is called again with the same parameters, we can optimize the program to use the previously computed result without re-calling the function. Because it knows that the call will not affect other parts of the program, such function calls can be freely moved and deleted.Of course, some friends might say, isn’t this just the definition of a reentrant function? There is still a difference; reentrancy and the categorization here are somewhat different.A reentrant functioncan read or even modifythe memory pointed to by pointers passed by the caller.
2
Functions that depend on external values but do not modify them
The return value of these functionsdepends on the values of their input parameters and/or the values of global variables and static variables.These functions alsodo not modify any global variables, static variables, or memory accessed indirectly through pointers (i.e.,no side effects). However, such functionscan read global variables, static variables, or memory accessed indirectly through pointers (if the pointer points to constant data or is not modified within the function). For example, the following function reads an external global variable.
int calculate_sum(int a, int b)
{ return a + b + global_constant; }// (reads global constant `global_constant`)
These functions not only need to consider parameters but also need to be cautious about how changes in global variables affect the function’s results.
3
Inline functions
Inline functions are a compiler optimization technique, and the core idea is todirectly “insert” the function’s code at each call site instead of performing a regular function call (stack push, jump, execute, return, stack pop). The purpose of this is toeliminate the overhead of function calls, thereby improving the program’s execution speed, especially when the function body is small and called frequently. For example:
// myheader.h
static inline int max(int a, int b) {
return (a > b) ? a : b;
}
<span><span>inline</span></span> is merely asuggestion, and the compiler ultimately decides whether to inline it. Functions with large bodies, containing loops, or recursive functions are usually not inlined (even if you write <span><span>inline</span></span>), so our company’s guidelines require that inline functions cannot exceed 10 lines. Of course, the newer versions of GCC can also enforce inlining, but the downside of inlining is that the compiled file becomes larger, so it should be used cautiously on resource-constrained platforms.Thus, categorizing functions during the initial design and marking them in the coding standards can help avoid many issues in future development~
Finally
That’s all I want to share with you today. If you found it helpful, please remember to give alike~
A unique, permanent, and free platform for sharing embedded technology knowledge~
Recommended Collections Click the blue text to jump
☞ MCU Advanced Collection 
☞ Embedded C Language Advanced Collection 
☞ “Bug Says” Collection 
☞ Collection | Comprehensive Programming for Linux Applications
☞ Collection | Learn Some Networking Knowledge
☞ Collection | Handwritten C Language
☞ Collection | Handwritten C++ Language
☞ Collection | Experience Sharing
☞ Collection | Power Control Technology
☞ Collection | From Microcontrollers to Linux
