Routine and Subroutine

Updated: 04/01/2018 by Computer Hope

A routine or subroutine, also referred to as a function, procedure, and subprogram, is a portion of code that may be called and executed anywhere in a program. For example, a routine may be used to 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. Below is a basic example of a Perl subroutine.

Subroutine example

&hello;

sub hello {
     print "Hello World!\n";
}

In the above example, a user could call the hello subroutine (in this example, by typing &hello;) anywhere within the program 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.

Call, Control flow, DLL, Function, Instance, Programming terms, Return address, Return statement, Sub, Stubroutine