In the sixth chapter we saw how to define and use functions that are C subprograms. It explained the definition of the functions, the call, the references to be made, as well as the mechanism of exchange of parameters and results. Throughout this chapter we will analyze more advanced concepts about functions and demonstratives, maya function, complex tasks and prototypes of functions.
Functions and demonstratives can be confused using C naturally. Therefore, it will be possible that the parameter of a function is the index, that the result of the function is demonstrative and that the functions are referenced by demonstrators.
Parameters will be demonstrative when parameters should be used by reference, for example, when the result should be left in the parameter (see program 1).
The result of a function, and therefore the function type, can be a demonstrator, for which you must specify the type of data shown to the left of the name in the function description and the * character (see program 2).
Finally, the demonstrators for the functions can be very interesting, since they allow us to execute a different function or routine according to a data or code. A clear example is the discontinuities vector or error handling module that appears in Program 3. In it, the names of the functions, like those of the tables, indicate their representative, so to define a variable that shows any function you must use the * symbol.
Using an assignment we locate the address of function f in the following program:
is a well-written text in which the address of f is located in the variable pf.
C programs should be the main Maya function, as it is the first to run. Although so far this master function of the program has been used without parameters, sometimes it may be interesting to obtain some data from the execution command of the operating system at runtime. For example, when we want to make a program that handles a file we have two options: to program it for a fixed file or for any file, since the name of the file will be specified in the execution command.
This second is much more general. When using this option, the argc and argv keywords should be specified as the main parameter as the argument. Then argc and argv, the first of int type, will be added, and for the second the expression char * argv [ ], an index table for character strings. Subsequently, the argc value during the program will be the number of arguments plus one specified in the command, while argv [1] will refer to the first argument, argv [2] the second, etc., obtaining the program name in argv [0]. This is a program called 4 echo that repeats what we write on the command line during execution (if the first word is the name of this program, of course).
In programs written in C there are sometimes complex reports in which it is difficult to know what the declared data is. Therefore, the errors described in the third program do not immediately mean the estimation of the variable.
void (* errors [2]) ()
The main reason for the difficulty lies in: * while the operator writes ahead [ ] and () behind, the agents of the table and function have more priority than those of the demonstrator, and the association is from left to right for agents [ ] and (), but vice versa for indicators.
The easiest thing to complete the definition is to follow the following rules:
() A table composed
by [... which returns...
* demonstrator for...
For example
charX is a table of demonstrators for characters
and
struct S(* (* (* (* (** x) [ ]] […]A table of demonstrators for the function returned by the demonstrator for the table formed by -structure S is a demonstrator in –x
In chapter six, the concepts related to functions are analyzed: definition, management and call. While the definition of functions determines the formal parameters, citing the corresponding types, the function call determines the instantaneous value of the parameters, but non-visible conversions occur, as very short numbers become ints and real double . This implies a loss of efficiency and there are no verifications in the exchange of parameters. Faced with this, some C compilers (following the ANSI C standard), besides mentioning in the functional reference the name of the function and the type of result, the data types of the parameters must be defined. For example:
extern void fun1 (int, float, char *);
is a function prototype specifying the type of the three parameters on which the function is based (int, float and char *). If we wanted there to be no checks on parameter exchange then externn void fun1 (), we would make reference.
Program 5 shows the occurrence of a build error due to parameter incompatibility.