Home

subroutine

A subroutine is a named sequence of program instructions that can be invoked from multiple points in a program. It accepts inputs, called parameters or arguments, and transfers control back to the caller when finished. Subroutines help modularize code, promote reuse, and reduce duplication by encapsulating a task in a single, reusable unit.

Historically, the term subroutine comes from early programming language design, where it competed with terms such

Implementation and behavior vary by language, but a subroutine generally has an interface (a name and a

as
procedure,
function,
and
method
to
describe
similar
concepts.
In
some
languages,
the
word
subroutine
implies
a
unit
that
does
not
return
a
value
(a
procedure),
while
in
others
it
is
a
neutral
term
encompassing
both
value-returning
and
non-returning
units.
The
distinction
between
subroutine
and
function
is
language-dependent;
in
some
environments,
a
function
is
a
subroutine
that
returns
a
value,
whereas
in
others
the
terms
are
used
more
interchangeably.
parameter
list)
and
an
implementation
(the
body).
When
called,
an
activation
record
or
stack
frame
is
created
to
hold
parameters,
local
variables,
and
the
return
address.
Parameters
may
be
passed
by
value,
by
reference,
or
by
other
mechanisms.
Subroutines
can
be
recursive
and
may
have
side
effects
on
global
state
or
I/O.
They
can
be
part
of
a
module
or
library
and
may
be
inlined
by
a
compiler
under
certain
conditions.
Effective
use
of
subroutines
supports
separation
of
concerns,
easier
maintenance,
and
reuse
across
a
codebase.