Download presentation
Presentation is loading. Please wait.
1
Subroutines Web Programming
2
Subroutine: Intro What is a subroutine? Why use subroutine? Syntax
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 Syntax subroutine definition sub subroutine-name { statements } subroutine invocation &subroutine-name; cycle -> iterative similar to system development cycle Web Programming
3
Subroutine: Forward Reference
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; When subroutine invocation is above subroutine definition, ‘&’ can be omitted if a forward reference is used. sub hello; hello; sub hello { print “hello.\n”; } cycle -> iterative similar to system development cycle Web Programming
4
Subroutine’s Return Value
Use Return Value to pass data from subroutine to main routine 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 return (return_value); Example returns return_value and exits subroutine return_value can be a scalar or a list cycle -> iterative similar to system development cycle Web Programming
5
Global & Local Variables
Global variables defined in the main routine exists everywhere in a program Local variables defined with ‘local’ or ‘my’ statement e.g. local $var1; my $var1; ‘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 cycle -> iterative similar to system development cycle Web Programming
6
Subroutine Arguments &subname (argument list)
use subroutine arguments to pass values from main routine to subroutines passed argument list is stored in &sub1($arg1, sub sub1 { my ($v1, … } Good programming practice Example use global and local variables use subroutine arguments when in doubt, use return statements cycle -> iterative similar to system development cycle Web Programming
7
Passing by Value vs. Reference
is copied # - i.e. operations will not @array = (1,2,3,4); &sub1 sub sub1 { my } Passing by Reference Example # reference is passed # is an alias in the subroutine # - i.e. operations will @array = (1,2,3,4); &sub1 sub sub1 { my ($list) } cycle -> iterative similar to system development cycle Web Programming
8
Passing arrays and hashes
All arguments passed to subroutine as a single list &sub1 will be empty sub sub1 { my } 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 cycle -> iterative similar to system development cycle Web Programming
9
Special Subroutines BEGIN subroutine END subroutine
executes at program start e.g. BEGIN { print “\n”; } END subroutine 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 cycle -> iterative similar to system development cycle Web Programming
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.