Function (programming)
In computer programming, a function is a sequence of program instructions that perform a specific task, packaged as a unit.
This unit can then be used in programs wherever that particular task should be performed.
Contents
Terminology
Similar terms include subroutine, procedure, routine, method, subprogram, callable unit, etc.
Exact usage may vary from one programming language to another, but all of these terms share common principles.
Description
Subprograms may be defined within programs, or separately in libraries that can be used by multiple programs.
A function is often coded so that it can be started (called) several times and from several places during one execution of the program. The program can then branch back (return) to the next instruction after the call once the functions's task is done.
Functions are a powerful programming tool, and the syntax of many programming languages includes support for writing and using them.
Judicious use of funtions (for example, through the structured programming approach) will often substantially reduce the cost of developing and maintaining a large program, while increasing its quality and reliability.
Arguments
Some functions use arguments. An argument is a value, or set of values, which you send to the function. The function then uses the value, or values, in some way.
Example, using PHP:
echo ("Hello World");
In the above example, "Hello World" is the argument.
Not all functions require (or accept) arguments. Example:
$t = time();
The use (or absence) of arguments depends on what the function does. Each case is different.
Libraries
Subroutines, often collected into libraries, are an important mechanism for sharing and trading software.
Object-oriented programming
The discipline of object-oriented programming is based on objects and methods (which are subroutines attached to these objects or object classes). See also Programming paradigm.
History
Maurice Wilkes, David Wheeler, and Stanley Gill are credited with the invention of this concept, which they termed a closed subroutine, contrasted with an open subroutine or macro.
See also
- Callback (computer programming) - a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time.
- Computer program
- Computer programming
- Framework (computing)
- Function (mathematics)
- History of the function concept
- Library (computing)
- Method (computer programming)
- Syntax (programming languages)
External links
- Subroutine @ Wikipedia
- Function definition @ rosettacode.org
- Call a function