A subroutine (sometimes referred to as a function, procedure, method or subprogram) is a programming language construct that performs a specific task and is relatively independent of the remaining code.
Many programming languages include support for self contained subroutines.
There are many advantages to breaking a program up into subroutines, including:
The components of a subroutine may include:
Subroutines are accessed by the main part of the program via a flow-control statement that performs a call to the subroutine. The format of such a statement in C++ is as follows:
name( arguments ) ;
where
name | is the identifier by which it is possible to refer to the subroutine. |
arguments | are the arguments passed to the subroutine. The number of arguments and their types must be the same in the subroutine call as they are in the declaration. |
; | closes the call statement and delimits it from the next statement. |
The call statement causes a transfer of control so that, instead of executing the next statement in the program, the computer executes the statements contained within the subroutine specified by name. When the subroutine has completed its task, it returns to the calling part of the program and execution continues with the statement following the call to the subroutine.
Many programming languages distinguish between a function, which returns a value (via a return statement which can be assigned), and a subroutine or procedure, which does not. Some languages do not make this distinction, and treat these terms as synonymous.
This web site uses these terms as follows:
In C++ and Java a function is declared as follows:
type name( parameters ) ;
where
type | is the data type specifier of the function's return value. |
name | is the identifier by which it will be possible to refer to the function. |
parameters | are the parameters which must be passed to the function when it is called. All the parameters for the function are collectively known as the parameter list. |
; | closes the declaration and delimits it from the next statement. |
The term method is commonly used in connection with object-oriented programming, specifically for subroutines that are part of objects.
A method is a subroutine which is a member of a class.
Methods represent an object's abilities. They consist of instructions which are carried out when the method is called, or instructed to run.
home | Home Page |