Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.

Similar presentations


Presentation on theme: "Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to."— Presentation transcript:

1 Scripting Languages Perl Chapter #4 Subroutines

2 Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to reuse code –They serve to generalize an algorithm that can be used on different data. Perl has several hundred builtin functions Now let’s learn how to write our own.

3 Functions / Subroutines Names mean the same – typically subroutines – the keyword sub Start by defining your functions at the start of your script. Use keyword sub followed by the function name Function names are again in different namespaces so &func1 is different from $func1

4 Example sub functionname { body of function } To invoke a function -- &functionname If arguments list them in ( ) following the name Inside the function the argument are accessed through a special Perl builtin array name @_

5 #!/usr/bin/perl –w #function.pl sub total { $sum = 0; foreach $item (@_) { $sum += $item; } return $sum; } @data1 = (1,2,3,4); $result = total(@data1); print “Sum of @data1 is $result\n”; @data2 = (1,2,3,4,5); $result = total(@data2); print “Sum of @data2 is $result\n”; $result = total(@data1, @data2); print “Sum of @data1 and @data2 is $result\n”;

6 Output %function.pl Sum of 1 2 3 4 is 10 Sum of 1 2 3 4 5 is 15 Sum of 1 2 3 4 and 1 2 3 4 5 is 25 % The function treats all arguments sent to it as one array Thus you can send an actual array, list of values or a combination Inside the function, there is simply a loop that traverses the builtin parameter array, @_

7 &functionname You can also use the & symbol to invoke or call a routine &total – would simply run the routine with not arguments All perl subroutines have return values Not all return values will be useful but it will attempt to evaluate a return value from the last statement of your subroutine

8 Be Careful The last expression may not produce the results you wanted Exp: sub total { $sum = 0; foreach $item = (@_) { $sum += $item; }

9 More than one value Subroutines can return a list of values as well – when evaluated in a list context Book provides a good example using the.. (range operator) – and reverse function

10 Arguments So we aren’t forced to use global variables We can pass arguments to a Perl subroutine $n = &max ( 10, 15 ); This list is passed to the subroutine – made available to use it can also be our special array variable @_ Confusing $_[0] is not $_

11 Example sub max { if ( $_[0] > $_[1]) { $_[0]; }else { $_[1]; }

12 @_ notes @_ is local to the subroutine if global @_ it is saved away before subroutine is invoked and restored to its previous value upon return this means you can transfer arguments to another subroutine with out overwrite

13 Private Variables By default all variables in Perl are global you can create private variables called lexical variables – using my operator These variables are private ( or scoped) to the enclosing block no other variables of same name will be changed – no other code will have access to these values

14 Example sub max { my ($a, $b); ($a, $b) = @_; #or my($a, $b) = @_; if ( $a > $b ) { $a} else {$b}

15 Variable – length Parameter Lists Parameter lists are not required in Perl It id good to make the subroutine adapt to the parameters $maximum = &max(3,5,10,4,6); sub max { my ($max_so_far) = shift @_ ; foreach ( @_ ){ if ( $_ > $max_so_far ) { $max_so_far = $_ ; } $max_so_far; } High-water mark algorithm – keeps track of the highest so far

16 use strict pragma Maybe you want Perl to be a little more strict use strict pragma provides this A pragma is a hint to the compiler use strict tells the compiler that is should enforce some good programming rules goes at the top – under the shebang #! From now on – add use strict to your scripts

17 return Operator immediately returns a value from a subroutine similar to C++ -- you don’t have to use it but it can be used throughout or within control structures where needed if return with no value – returns undef in a scalar context


Download ppt "Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to."

Similar presentations


Ads by Google