Home

subroutines

A subroutine is a named sequence of instructions within a program that can be invoked from multiple places. It accepts zero or more input values, may produce a result, and returns control to the caller after completing its task.

In many languages, the terms subroutine, procedure, and function refer to similar concepts, with variation in

Subroutines enable modularity, reuse, and clarity by encapsulating a task. They typically have parameters, which may

Control flow transfers when a subroutine is called and returns when finished. Subroutines may be recursive,

Implementation notes: Subroutine calls carry overhead compared to inlined code. Techniques such as tail call optimization

History and usage: Subroutines appeared in early procedural programming and remain fundamental for structuring programs, enabling

whether
a
return
value
is
produced.
Object-oriented
languages
often
use
the
term
method
for
subroutines
associated
with
objects.
be
passed
by
value
or
by
reference,
and
local
variables
whose
scope
is
limited
to
the
subroutine.
A
call
creates
a
frame
on
the
call
stack.
where
a
subroutine
calls
itself.
Proper
termination
and
base
cases
are
essential
to
avoid
infinite
recursion
and
stack
overflow.
and
inlining
affect
performance.
Languages
differ
in
parameter
passing
conventions
and
how
activation
records
are
structured.
abstraction
and
reuse.
They
are
implemented
in
many
forms,
including
standalone
procedures,
functions,
and
methods
within
libraries
and
APIs.