Subroutines Web Programming 1 Subroutine Intro n What

Subroutines Web Programming 1

Subroutine: Intro n What is a subroutine? ► n a separate body of code that perform a specific task Why use subroutine? ► Divide & Conquer: Example • easier to read/write program ► Repeated Task Example • write once, use anywhere n Syntax ► ► subroutine definition subroutine-name { statements } subroutine invocation &subroutine-name; Web Programming 2

Subroutine: Forward Reference n n Subroutines can be placed anywhere in a program, but it is best to put them before or after the main routine If subroutine invocation is below subroutine definition, ‘&’ is not needed. sub hello { print “hello. n”; } hello; n When subroutine invocation is above subroutine definition, ‘&’ can be omitted if a forward reference is used. sub hello; sub hello { print “hello. n”; } Web Programming 3

Subroutine’s Return Value n Use Return Value to pass data from subroutine to main routine ► n e. g. $price = &get_prices; Default Return Value ► ► Example The value of the last expression evaluated in subroutine Better to be explicit Example • return_value; #last line of subroutine n return (return_value); Example ► ► returns return_value and exits subroutine return_value can be a scalar or a list Web Programming 4

Global & Local Variables n Global variables ► ► n defined in the main routine exists everywhere in a program Local variables ► defined with ‘local’ or ‘my’ statement • e. g. local $var 1; my $var 1; ► ► ‘my’ variable exists only in the subroutine where it is declared ‘local’ variable exists in the subroutine where it is declared and subroutines that are called by that subroutine Example Web Programming 5

Subroutine Arguments n &subname (argument list) ► ► ► use subroutine arguments to pass values from main routine to subroutines passed argument list is stored in ‘@_’ &sub 1($arg 1, “yes”, @array 1); sub 1 { my ($v 1, $v 2, @list 1) = @_; … } n Good programming practice ► ► ► Example use global and local variables use subroutine arguments when in doubt, use return statements Web Programming 6

Passing by Value vs. Reference n Passing by Value # @array is copied to @_ # - i. e. operations on @list will not affect @array = (1, 2, 3, 4); &sub 1 (@array); sub 1 { my (@list) = @_; } n Passing by Reference Example # reference to @array is passed to @_ # - @$list is an alias for @array in the subroutine # - i. e. operations on @list will change @array = (1, 2, 3, 4); &sub 1 (@array); sub 1 { my ($list) = @_; } Web Programming 7

Passing arrays and hashes n All arguments passed to subroutine as a single list &sub 1 ($var 1, $var 2, @array 1, @array 2); # @list 2 will be empty sub 1 { my ($v 1, $v 2, @list 1, @list 2) = @_; } n Good programming practice ► ► list scalar arguments first pass arrays and hashes by reference when appropriate • large array/hash • multiple array/hash • need to modify array/hash values Examples #1 #2 Web Programming 8

Special Subroutines n BEGIN subroutine ► ► n END subroutine ► ► ► n executes at program start e. g. BEGIN { print “n”; } executes at program termination e. g. END { print “n”; } END subroutine executes even after “die” AUTOLOAD subroutine ► executes when non-existing subroutine is called e. g. AUTOLOAD { print “subroutine $AUTOLOAD not found. n”; } Example Review: Hash check example Web Programming 9
- Slides: 9