Functions in C
Top-down structured design is a methodology for creating complex programs. The principle, relatively standard, is to decompose a complex problem into a set of simpler problems and to solve each of these simpler problems (applying the same principle if needed). This methodology is applied in a large number of domains and for a great variety of problems: project management, construction management, games, strategies…
The objectives and benefits of such a methodology are multiple:
- simplification: allows tackling complex problems by breaking them down into a succession of simpler tasks which are themselves decomposable into tasks or elementary instructions.
- abstraction: allows focusing on the core of the problem by avoiding technical details or language-specific features.
- structuring: facilitates program maintenance by making it easy to locate certain features or parts of the code.
- reuse: certain tasks can be adapted from other problems and therefore this methodology allows reusing what has already been written (cf
printf,scanf…).
For this, parameterized functions are introduced which allow performing tasks that are repeated frequently. These functions are subprograms that can be called by the main program or by another subprogram. These tasks can modify their behavior based on certain values: the parameters (cf printf, scanf…).
Functions
Definition
:::warning Function Definition A function is a subprogram that from a (possibly empty) set of inputs produces one and only one result. :::
Here, using an example, is the syntax for defining a function. The function determines the maximum of two integers.
int max(int a, int b)
{
int m;
if (a < b) m = b;
else m = a;
return m;
}
A number of things are noteworthy about this example:
- the first line indicates what the return type of the function is (here
int) since a function must always provide a result, what its name is, and what the parameters of this function are along with their types. All parameters present on the first line (between parentheses) must necessarily be defined. - then local variables are defined, i.e., temporary variables, useful only for the function.
- then comes the body of the function: a set of instructions
- finally the keyword
returnfollowed by a variable or expression indicates what value the function returns. Of course the variable/expression followingreturnmust be of the same type as that specified on the first line.
:::info Note
It is possible to have the keyword return multiple times in a function; only the first one encountered matters, the others are not reached.
:::
Call
Once defined, this function can be used by a program or a subprogram. As in the following example where the function max is called with different sets of parameters:
int main()
{
int x = 5, y = 19, z;
z = max(4, 7);
z = max(4, x);
z = max(x, y);
z = max(2 * x, y);
return 0
}
:::info Vocabulary To avoid confusion, two types of parameters are defined for functions and procedures:
- Formal parameters: these are the variables used in the body of the subprogram (for example
aandbin themaxfunction). - Actual parameters: these are the value/variable/expression evaluated and provided when calling the subprogram (for example
4and7for the first call ofmax) :::
Calling the function max in the program above implies a number of steps:
- the actual parameters of the function are replaced by their value if they are variables or expressions. Thus
maxis successively called with(4, 7),(4, 5),(5, 19),(10, 19) - these values are assigned to the formal parameters of the function, the first value going to variable
a, the second tob. Thus for the first call ofmax,aequals4andbequals7. - the body of the function is executed until the keyword
returnis encountered - the result returned by the function is the value of the expression following
return. For the first call ofmax, the returned value is4. - this returned value is assigned to a variable (here
z).
Procedures
In some cases, the subprogram does not need to return a value (for example to print things to the screen). In this case we speak of procedures and in C, the syntax is the same as for a function except that:
- the return type is
void returnstatements are not followed by any value/variable/expression
For example:
#include <stdio.h>
void print_max(int a, int b)
{
int maxi;
if (a < b) maxi = b;
else maxi = a;
printf("Le maximum de %d et %d est : %d\n", a, b, maxi);
}
Limitations
The function principle in C only allows returning at most one value. This is a limitation when one would like to return multiple values with a single function. We will see in a later chapter (i.e., on pointers) how to work around this limitation. The proposed solution will also address another problem: certain complex types cannot/should not be returned.
Summary
Here is what to remember about functions and procedures
:::warning Function A function returns one and only one result. :::
// declaration (optional)
return_type function_name(param_list);
// definition
void function_name(param_list)
{
//instruction_list
}
// call
compatible_type_variable = function_name(expression_list);
:::warning Procedure A procedure does not return a result. :::
Syntax:
// declaration (optional)
void action_name(param_list);
// definition
void action_name(param_list)
{
instruction_list
}
// call
action_name(expression_list)