Routine
![Illustration of programming routine.](/jargon/m/method.jpg)
A routine or subroutine, also called a function, procedure, method, and subprogram, is code called and executed anywhere in a program. For example, a routine could save a file or display the time. Instead of writing the code each time these commonly performed tasks are needed, routines are created and called when these tasks need to be performed.
Subroutine examples
Below is a basic example of a Perl subroutine.
&hello; sub hello { print "Hello World!\n"; }
In the example above, a user could call the "hello" subroutine (in this example, by typing &hello;) and have the program print Hello World!. After the subroutine has completed, the program goes to the return address, which is directly after &hello; and runs any additional code.
Below is an example using JavaScript methods. An object named "GPS" (Global Positioning System) is created, and within that object, a method named send_msg is defined.
function GPS(msg) { this.send_msg = function() { window.alert(msg); } } var lost = new GPS("You are lost!"); lost.send_msg();
The send_msg method sends a message stored with each instance of the GPS object as a pop-up alert to the user when run. In this case, when the code is run, the user gets a pop-up message that says "You are lost!".
Call, Class, Control flow, DLL, Function, Instance, Library, Object, Programming terms, Return address, Return statement, Snake case, Stubroutine, Sub, Variable