Page 67 - Programming Microcontrollers in C
P. 67

52     Chapter 1  Introduction to C

                              A function may not be nested inside another function. Therefore,
                          any function must be created outside of the boundaries of any other
                          function or program structure. Functions have only one entry point,
                          and they return only one return item. The return can be of any type
                          that C supports. Functions can have several arguments. The argu­
                          ments can be of any valid C type.
                              In ANSI C, the use of a function requires the use of a function
                          prototype. A function prototype is a statement of the following form:
                   type function_name(type, type, type,...);

                          The first type preceding the function name is the type to be re­
                          turned from the function. It is also called the type of the function.
                          The several types found in the argument are the types of the corre­
                          sponding arguments that are sent to the function. Variable names
                          may or may not be used for the arguments of a function prototype.
                          The type list is the important item. The types in the argument list are
                          separated by commas.
                              Thus far, it might seem that we have been blindly using functions
                          like printf(), getchar(), and putchar() without the ben­
                          efit of function prototypes. Not so! The header file stdio.hcontains
                          the function prototypes of all input/output related functions, so it is
                          not necessary for you to put a function prototype in your code for
                          these functions. Other library functions have their prototypes in their
                          own header files.
                              Compilers will differ. If a programmer attempts to send the wrong
                          type of data to a function through its argument, the compiler might
                          consider it an error or it might well convert the argument to the cor­
                          rect type prior to calling the function. In either case, the compiler
                          will not let a program use the wrong type of data as an argument to a
                          function. The standard allows that the parameter type be corrected to
                          the correct type and proceed. Some embedded systems compilers
                          will require that the type of each parameter be correct before the
                          program can compile.
                              One item that is important. Copies of parameters are passed to
                          any function. Copies are placed on the system stack or in registers or
                          both before the function call is executed. Therefore, the program can
                          use these parameters in any way without altering the calling pro­
                          gram. In fact, after you have done what is needed with a parameter,
                          you may use the parameter as a storage location for your function.
   62   63   64   65   66   67   68   69   70   71   72