in

Difference between procedure and function

Knowing when to use a function and when a procedure is one of the most frequent dilemmas among beginners in programming. We do not refer to the library functions of programming languages ??or to procedures that the user must perform to achieve something. We refer to the functions and procedures defined by the user.

Although some languages ??offer only an alternative for modulation – such as C, for example, which only allows the use of functions – many of those commonly used in teaching offer more than one alternative to implement the modularity –Pascal and Visual Basic are two good examples.

In this article we will mention the most important general differences between functions and procedures without particularising in any programming language. The following can be adapted for any other language that allows defining different types of subroutines, and is aimed at completing the basic knowledge on the subject that can have the newcomers in programming.

The similarities

Before enumerating the differences between functions and procedures, we will mention the similarities that exist between these two types of routines. Although this is not the primary objective of this document, it will be very useful to focus on the differences:

Main Similarities between Functions and Procedures

  • Both types of subroutines are defined by the user (the programmer)
  • Both types can be defined to accept arguments (parameters)
  • Both types can be invoked in various ways for execution and from several points within the main program
  • Both types can return results to the call point
  • Both types can be grouped into subroutine libraries that other programs can include

Now that we have established similarities, let’s go to action.

The differences

Functions can only return a single value, procedures more than one.
Although this is what the theory establishes, in practice it can be done that the functions return more than one value by passing pointers as parameters by value, each of which can point to the initial memory address of a data structure complex, such as an arrangement or a record. Since in most languages ??only a single value (the one specified in the declaration / definition of the function) is allowed to be returned, this trick allows the programmer greater versatility without violating the syntactic rules of the language itself. Another way to circumvent this limitation is to use global variables, although it is advisable to document well each instruction where they are used as well as to keep them under strict supervision to avoid side effects.

Functions only accept input parameters. The procedures accept both input and output parameters. 
This difference is fundamental and is related to the previous one. It gives the procedures more generality and versatility, as mentioned in the following difference.

The procedures may contain other subroutines. Functions do not. 
A procedure can include all those functions you need to perform your task (s), as well as other procedures. In object-oriented programming, this feature facilitates encapsulation, one of the key concepts of this programming philosophy. In this way, a procedure can be converted into a module, which can contain up to its own data stores and function independently within the structure of a program.

The procedures are autonomous. Functions do not. 
A function can only be called execution when it is included as part of a statement or as an argument for another function. That is, it alone can not be constituted as an independent sentence. Procedures on their part, although they may behave similarly to functions in some cases, may constitute an independent judgment in and of itself. The following examples in Pascal will help you understand this point.

Examples in Pascal of Call to Functions
Tax: = Calculate_IVA (Amount);
Total_a_Pay: = Amount + Calculate_IVA (Amount);
WRITELN ('Tax payable:', Calculate_IVA (Amount));

Pascal Examples of Procedural Calls
Calculate_Area (Base, Height, Area);
WRITELN ('Area of ??the rectangle:', Calculate_Area (Area, Base, Height));

A function must specify the type of data to be returned. The procedures do not. 
When declaring / defining a function, it is mandatory to include the type of data that this function will return to its point of call. This is very important since the variable, function or expression that receives the value returned by the called function must be able to handle the type of data that the latter produces. Otherwise you will get the typical “Type mismatch” error at compilation or execution time.

The data type of the receiver variable, that of the components involved in the expression, or that of the argument that is substituting the called function, need not necessarily be of the same type. They can be superior in rank within, always within the same category. For example, a REAL variable of Pascal can accept an INTEGER or BYTE value ; in C a double variable can hold a long or float value .

Two last differences related to style and tradition, and not to the very structure of functions and procedures are as follows:

The functions are intended to perform a single task. Procedures for more than one. 
This restriction is oriented more to favor the style and to maintain simple the design of the programs. Ensuring that a function performs a single thing also favors debugging and maintenance. In practice it can be done that a function does more than one thing, but it is not recommended for the aforementioned advantages. The procedures, on the other hand, since they are more general and can return more than one value to their call point through the input / output parameters (parameters per variable in Pascal) are often recharged with more than one task .

Functions should be short. The procedures are usually long.
Since the functions are used for specific and specific tasks, they are often short. Good programming practices recommend that a feature should not overlap the two text screens so that it can be manageable and kept simple. Beyond this measure must be considered its fragmentation in two or more subfunctions. Procedures instead, as they could incorporate other functions and procedures and act as modules or classes within a program, often span multiple pages. However, beyond 10 usually leads to seriously consider its fragmentation or refactoring in order to improve the design and favor purification and maintenance

Difference between production and productivity

Difference between product and service