Download presentation
Presentation is loading. Please wait.
1
Perl Functions
2
Defining Functions sub subroutine_name { stmt1; stmt2; … }
Arguments are not listed in declaration May use a return statement If no return statement, value of last expression is returned Can return scalar, array, or hash
3
Calling Functions Called by
sub_name(arg1, arg2, …); Parens may be omitted if declaration has already been seen Arguments are available in called function as entries in
4
Sample Function sub max { ($a, $b) = @_; $a > $b ? $a : $b; }
Puts args into variables Returns larger if they are numbers
5
Array and Hash Arguments
Can only pass one array or hash as an argument unless you know the size Args are flattened @b) #all Can precede array or hash by a fixed number of scalars but $c) won’t work
6
Scope my($a); my Declares local variable with scope of block (including function) or loop (in control Parentheses are optional
7
Strict use strict; Directive requires Declare all variables
Declare all functions before first use
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.