Functions in C language

F

Functions divide complex tasks into small pieces easier to understand and program. These can be reused on other occasions, instead of being rewritten from scratch.

Functions are also useful to hide the details of running certain parts of the program, helping it work. By using functions, which represent the fundamental execution unit of C programs, a logical division of large and complex programs is obtained. The division of programs into functions is arbitrary and depends on the thinking of the person who creates them. Typically, the functions include a series of instructions that perform a calculation, perform an action, implement an algorithm.

The creation of functions must be based on the following principles: clarity, legibility, ease of maintenance, reusability. The defining features of a function in C are: name, call parameters, and return value.

The standard syntax of declaring a function is:
type_returned function_name (type_param1 param1, type2_param2 param2);
This statement is called a function signature or a simple signature. The list of parameters may be missing. Once declared, a function must be defined in the sense that its body must be expanded with the instructions it must execute.

Defining a function has the form:
type_returned function_name (type_param1 param1, param type2 param2, …) {
  statement of variables and instructions;
  return expression;

The C language separates the declaration of a function from its definition (the code that implements it). For the function to be used, it is only mandatory to declare it before the calling code. The definition may appear further in the source file, or even in another source file or library.

Different parts of defining a function may be missing. Thus, a minimal function is:
dummy () {}

The above function does nothing, returns no value, and does not get any arguments, but from the point of view of the C language is perfectly valid. The type returned by a function can be any standard or user-defined type including the void type (which means that the function does not return anything).

Any function that returns a result must contain the statement:
return expression;

An expression is evaluated and converted to the type of data to be returned by the function. This instruction also terminates the execution of the function, whether or not there are other instructions afterward. If necessary, multiple return statements can be used to determine more points of exit from the function in relation to the evolution of the function.

The call to a function is made by specifying the actual parameters; the parameters that appear in the declaration of the function are called formal parameters. The call to a function is made by specifying the parameters to be transmitted to it. In C, but also in other programming languages, there are 2 modes of parameter transmission.

1. Send parameters by value:
The function will work with a copy of the variable it received, and any change in the function will work on these copies. At the end of the execution of the function, the copy will be destroyed, and any modification will be lost. In order not to lose the changes made, the return statement is used, which can return the new value of the variable, upon completion of the function. The problem occurs if the function changes multiple variables, and it is intended that their result be available at the end of the function execution.

2. Transmission of parameters via memory address
A function can also turn to other functions. If a function appeals to itself, then the function is recursive. To avoid an infinite number of recursive calls, the function must include a stop condition in its body, so that at some point the recurrence stops and returns successively from calls. Condition must be generic, and stop recurrence in any situation. This condition generally refers to the input parameters, for which at a given moment the response can be returned directly without the need for an additional recursive call.

Recent Posts

Archives

Categories