Programming language C (VI). Functions and Macros
1991/07/01 Alegria Loinaz, Iñaki | Maritxalar, Montse Iturria: Elhuyar aldizkaria
Functions of functions Functions
As with other high-level languages, C allows us to write subprograms. According to the language, the subprograms are called differently, being the procedure, the function, the routine, the most common subroutines. The different denominations differ by different characteristics of little importance. Therefore, when we say function, for example in the case of C, the subprogram is supposed to implicitly return a result. The procedure does not return implicit results.
However, the objective of the functions of C, like that of all the subprograms, is to facilitate the design and writing in the program, since with them it is possible to separate a complex problem in other simpler ones, solving each one with a simpler program. This technique, called descending or modular programming, has at present a great diffusion for the improvement of the efficiency (both in the development and in the correction) of the programmers.
Let's look at an example, like the case of the factorial. In the two previous chapters we have carried out different programs that calculate the factorial, but always in a single program. This is not the only option, since the calculation of the factorial can be programmed definitively in a subprogram or function and be used from different main programs as explained in program 1.
![](images/A50029.jpg)
In program 2 you can see the use of the same function,
m / (m - n)! n n n n n n
To calculate the expression through the formula (m / n).
In the second program we have not written the factorial code and must indicate to the compiler that the function will be compiled separately. Therefore, the type of factorial must be indicated, specifying the external distinction. With this scheme, the object module that is generated when compiling the definition of the factorial function is linked to the one created by the main program, obtaining a single executable program.
![](images/A50029-a.jpg)
As seen in the examples, if we use the functions in programs C, we can make allusions of three types: definition, call and development.
Definition of the function
It serves to express what the subprogram does, specifying the type of function result, the name, the arguments, the definition of the arguments and the body.
If there is more than one argument, they are distinguished by character. In Example 1 the name of the function is factorial, the type of result is long, the argument n, the definition of the argument int n, and everything that is next to the body up to the main program. Note that the main program is a function called main without argument.
The format of the definition is as follows:
![](images/A50029-b.jpg)
If the function returns results, it will be done through the return sentence, which is normally the last of the body. If the function does not return results (in other languages it is called procedure), the void will be written as a type of result, since if the type of result is not mentioned, the entire number will be returned.
The definition of the arguments is performed as a definition of the data. The only difference is that these data are sent from other functions (or from the main program). Therefore, these arguments are called formal and when calling the function, they will be filled with specific data.
In the body of the function, the code of the subprogram is indicated, indicating both the definition of your data and the instructions.
Let's write the definition that returns the largest number in the two integers: we invented the name, the big ones, for example, the parameters are two and also the two integers (the calls a and b) and the type of result is one of the two numbers, so it will be also integer. Therefore, the head of the function will be:
big int (a, b)
int a, b; /*function parameters */
The body is very simple and once performed here the local variable called result will be used.
1st result result
/*Local variable*/ if (a b) result = a; else result = b; return (result)
Budgets for budgets Budgets
Call of the function
When you want to run a function, a call is made, so that with the data indicated in the call (parameters) the code corresponding to the definition of the function is executed and the result is returned.
If the function returns results, the call will normally appear on the right of an assignment. However, it can also appear in the middle of an expression or as a parameter of another function.
Think that we have read two numbers in the variables z1 and z2 and want to write the largest. We have two options:
a) int em; ... em = large (z1, z2) printf (“%d”, em); ... b) printf (“%d”, large (z1, z2);
In program 2 described initially it can be observed that the factorial function is called three times in the same program, but using different parameters.
When you need to get more than one result in a subprogram, you cannot do it with return judgment (this allows you to return a single result). This will be done through the parameters defined with reference (see chapter 10).
Development of the function
When in a program we call a function that is defined in another module, the function must be described so that for lack of definition the compiler does not give error. This has been done in Example 2 and what should be specified in the report is: to indicate that the external keyword is defined externally, to differentiate the type of result, the name of the function and the function ( ). In the example we are dealing with, the explanation would be as follows.
large externn int ( );
When the function and the call are within the same module, it is not necessary to perform the design of the function, if the definition of the function is written before the call, but it is necessary to send but without entering the external keyword.
Macros Macros
This programming tool, so common in assembly languages, appears in a few high level languages. For example in C.
Macros are used to define and refer simple subprograms, but they present the following functional differences:
- Calls do not imply the execution of the subprogram. On the contrary, the compiler replaces a call to the body of the definition. Therefore, during the compilation the calls will disappear and a new code will be compiled.
- Executable programs are longer, but faster.
- If parentheses with parameters are not used, there may be problems.
- are not appropriate for complex subprograms.
Define and refer (call) macros. The format of the definition is as follows:
![](images/A50029-c.jpg)
And the case of the elder we would do it like this:
#define large (x, y) ((x) (y)? (x) : (and)
if you do not have an option
Instead of the structure, ? : the expression has been used, since otherwise the result cannot be returned. The references are made just like the calls of the function, although the way of influencing is very different.
With this reference,
em = large (zenb1, zenb2);
The substitution of compiler in machine language will result in:
em = ((zenb1) (zenb2)? (zenb1) : (zenb2)
This is because the compiler, seeing a macro name, substitutes it for the macro body, replacing the formal arguments with reference parameters.
As can be seen, the mechanism is smaller than that of the functions, so it is rarely used.
To finish, we will place a macro to calculate the surface of the circle:
#define PI 3.1416
# defines area (r) PI *(r)*(r)
![](https://elia.eus/static/elhuyar/img/elia_RGB.png)
Gai honi buruzko eduki gehiago
Elhuyarrek garatutako teknologia