FortranProcedures - Functions and Subroutines


Remarks

Functions and subroutines, in conjunction with modules, are the tools to break down a program into units. This makes the program more readable and manageable. Each one of these units can be thought of as part of the code that, ideally, could be compiled and tested in isolation. The main program(s) can call (or invoke) such subprograms (functions or subroutines) to accomplish a task.

Functions and subroutines are different in the following sense:

  • Functions return a single object and - usually - don't alter the values of its arguments (i.e. they act just like a mathematical function!);
  • Subroutines usually perform a more complicated task and they ordinarily alter their arguments (if any is present), as well as other variables (e.g. those declared in the module that contains the subroutine).

Functions and subroutines collectively go under the name of procedures. (In the following we will use the verb "call" as synonym of "invoke" even if technically the procedures to be called are subroutines, whereas functions appear as right hand side of assignment or in expressions.)