Difference between Subroutine, Procedure & Function?
What is a Subroutine?
A subroutine is a piece of code executed upon demand, separate from
the main flow of the program. The interpreter will jump to the code,
execute it, and return to the main program.
Keywords such as GOSUB and RETURN, from the BASIC language, often
used with line numbers, provided a programmer-friendly way to achieve
this. The assembly language equivalents were less easy to digest,
usually comprising a comparison, jump (or branch) and return, complete
with offsets, memory addresses, or, in more modern assemblers, labels.
Subroutines are considered by many to be hard to maintain, difficult
to read and digest, and are held in a similar light to the GOTO
statement. In other words – reserved for use when nothing else will do.
It is worth noting that a modern language, implementing a modular
, with named labels rather than line numbers, might be able to
include subroutines in an acceptable fashion. However, with functions
and procedures available, there may be no call for this approach at all.
Generically, the term subroutine can be used to denote a
piece of code, separate from the main body, fulfilling a discrete task,
in language-neutral terms. For example, a document might refer to the file handling subroutines.
What is a Procedure?
A procedure is a named block of code, like a subroutine, but with
some additional features. For example, it can accept , which
might be input, output, or pass-through.
Traditionally, a procedure returning a value has been called a
function (see below), however, many modern languages dispense with the
term procedure altogether, preferring to use the term function for all named code blocks.
Subsequently, the keyword PROCEDURE exists only in certain
languages, and has disappeared from many. It is worth noting that
languages like C do not use it, and that BASIC based languages do,
whereas Modula and Pascal based languages have both the PROCEDURE and
FUNCTION keywords, in which a FUNCTION is a PROCEDURE that returns a
What is a Function?
As we saw above, a function is considered in some languages to be a
procedure that returns a value. However, it is usually the term used to
refer to any named code block. It is worth noting that C based
languages use the function keyword exclusively.
Functions can accept parameters, return values, and are usually
maintained separately from the main program code. Many programming
languages have a special kind of function (in C, the main function) designated as the entry point to a program.
BASIC based languages have no such entry point, since they are
entirely procedural in that execution begins at the top of the program,
and continues until it is told to stop.
- None Found