Page 69 - Programming Microcontrollers in C
P. 69

54     Chapter 1  Introduction to C

                              The above argument is valid for microcontroller applications code.
                          It does not necessarily follow for code written for large computers.
                          When writing for a large computer, there are usually few memory
                          constraints. In those cases, it is probably best to use more function
                          calls and not be worried about the memory space taken up by func­
                          tion calls unless there is a serious speed constraint. When speed is a
                          problem, the programmer must go through an analysis similar to that
                          above with the dependent parameter being time rather than memory
                          space. In small computers where several registers can be saved and
                          restored when a function is entered and exited, single instructions
                          can require many clock cycles. When deciding whether to use a func­
                          tion or in-line code, the programmer must assess the total time lost to
                          entering and exiting a function each time it is entered, and weight
                          that time lost as a fraction of the total time the program resides in the
                          function. If this time is large, and the program requires too much
                          execution time, consider the use of in-line functions.
                              It is always good to write small functions and create simple call­
                          ing programs to exercise the small functions. These programs are
                          used to debug the functions, and they are discarded after the func­
                          tions are debugged. If later, the program constraints dictate that in-line
                          code should be used, the essential code of the function can be written
                          into the program wherever it is needed. Another approach that will
                          be discussed in the next chapter is to use a macro definition to specify
                          a small function. With a macro definition, the function code is writ­
                          ten in-line to the program whenever the function is invoked.
                              Let us revisit an example used earlier. Write a program to calcu­
                          late and display the square root of each integer less than 11:
                   /* Calculate and display the square roots of
                   numbers
                   1 <= x <=10 */


                   #include <stdio.h>

                   #define abs(t) (((t)>=0) ? (t) : -(t))
                   #define square(t) (t)*(t)

                   double sqr ( double );
                   int main(void)
   64   65   66   67   68   69   70   71   72   73   74