Perl subroutines encapsulate blocks of code in the usual way. You do not need to define subroutines before they are used, so Perl programs generally have their “main” code first, and their subroutines laid out toward the bottom of the file. A subroutine can return a scalar or an array.
$x = Three(); ## call to Three() returns 3
exit(0); ## exit the program normally
sub Three {
return (1 + 2);
}