There are several important points to keep in mind when writing functions for C, because they affect the efficiency and usability of functions.
4.5.1 parameters and general functions
Common functions are functions that can be used in various situations, or that can be used by many different programmers. We should not build common functions on global variables (global variables should not be used in common functions). All data required by a function should be passed with parameters (static variables can be used in cases where it is difficult to do so individually). Using parameter passing,
In addition to helping the function to be used in a variety of situations, it can also improve the readability of the function code. Without global variables, you can make a function less likely to cause errors due to side effects.
4.5.2 efficiency
function is the basic component of C language. Functions are essential for all programs that are written in addition to simple programs.
However, in some specific applications, the function should be eliminated, and the embedded code should be used. Inline code refers to the statement of a function that does not contain a function call statement. Use inline code instead of a function only if the execution speed is critical.
There are two reasons why the inline code executes faster than the function. First, the call takes time, and secondly, if there are parameters that need to be passed, it takes time to put them on the stack. In almost all applications, the small overhead of execution time is negligible. However, when time overhead is critical, use inline code to eliminate function calls,
You can save the overhead of each function call. The following two programs print the squares of the numbers from 1 to 1 0. Because the function call takes time, the inline code version runs faster than the other.