Page 254 - Introduction to Microcontrollers Architecture, Programming, and Interfacing of The Motorola 68HC12
P. 254

8,5 Procedures and Their Arguments                                  231


        element of the structs in the table. Index addressing is useful for accessing elements in a
        row of a table. If the address register points to the first word of any row, then the
        displacement can be used to access words in any desired column. Also, autoincrement
        addressing can be used to select consecutive words from a row of the table.
            In C, a table tbl is considered a vector whose elements are structures. For instance,
        the declaration
                        struct {char ll;int 12;char 13;} tbl[3];

        allocates a table whose rows are similar to the list list above. The dot notation with
        indexes can be used to access it, as in
                                     a = tbl[2].11;

        In simple compilers, multidimensional arrays and structs are not implemented. They
        can be reasonably simulated using one-dimensional vectors. The user becomes
        responsible for generating vector index values to access row-column elements or struct
        elements.
            Finally a comment is anything enclosed by / * and * / . These can be put
        anywhere in your program, except within quotation marks. Alternatively, everything
        after / / on a source line is considered a comment. However, the latter syntax is not
        available on all C compilers.


        8.5 Procedures and Their Arguments

        A procedure in C may be called by another procedure in C as a procedure. The arguments
        may be the data themselves, which is call by value, or the address of the data, which is
        call by name. Call by reference is not used in C (it is often used in FORTRAN).
        Consider the following example: RaisePower computes i to the power j , returning
        the answer in k where i, j, and k are integers. The variable i is passed by value,
        while j and k are passed by name. The calling procedure would have
        RaisePower (i, & j, &k); and the called procedure would have
                   void RaisePower (int i, int *j, int *k) { int n;
                       for(*k =1 , n = 0; n < *j; n++) *k =* k * i;
                   }
            Formal parameters are listed after the procedure name in parentheses, as in (i j,k),
        and in the same order they are listed after the procedure name as they would be for a
        declaration of local variables. However, they are listed before the curly bracket ({).
            Call by value, as i is passed, does not allow data to be output from a procedure,
        but any number of call by value input parameters can be used in a procedure. Actual
        parameters passed by name in the calling procedure have an ampersand (&) prefixed to
        them to designate that the address is put in the parameter. In the called procedure, the
        formal parameters generally have an asterisk (*) prefixed to them to designate that the
        data at the address are accessed. Observe that call by name formal parameters j or k
        used inside the called procedure all have a prefix asterisk. A call by name parameter can
        pass data into or out of a procedure, or both. Data can be input into a procedure using
        call by name, because the address of the result is passed into the procedure and the
   249   250   251   252   253   254   255   256   257   258   259