Subroutines
homencsa resources partnershipsoutreachsoftware_tech
alliance searchinside ncsa adv_computingsciencedivisions
left up right comment

Subroutines

What's a language without subroutines? Perl subroutine definitions take the following form:
	sub NAME { 
		STATEMENTS; 
	} 
In perl, subroutines are invoked differently than the builtins
	&NAME(arg1,arg2,etc); 
	# - or - (if the subroutine takes no arguments. 
	&NAME; 
	# return values are used just like anyother value  
	$loudestInstrument = &measureLoudest(@allInstruments); 

Example

	# define an error routine: 
	sub error { 
		($message) = @_; 
		print("<b>ERROR:<b>", 
			$message, 
			"<p>Contact the author of the previous page for assistance\n"); 
		exit(0); 
	} 
	if ( ! $recipient ) { 
		# the form did not have a to field 
		# modify this text appropriately 
		&error("No Return Address"); 
	} 
Note how the definition of the subroutine doesn't affect the flow of our program.
left up right comment



NCSA
The National Center for Supercomputing Applications

University of Illinois at Urbana-Champaign

johnsonb@ncsa.uiuc.edu

Last modified: June 19, 1997



Retrieved by Memoweb from http://www.ncsa.uiuc.edu/General/Training/PerlIntro/subroutines.html at 08/02/99