Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.

Similar presentations


Presentation on theme: "Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return."— Presentation transcript:

1 Lecture 8: Basic concepts of subroutines

2 Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return value – } – After all the subs are defined they are then called as required: my $variable = subname($name); The SubroutineExample.pl

3 Functions: passing more than one variable Subroutines : pass one or more variables. – sub subname – { my ($var1, $var2,….) = @_; { alternatively uses subscripts $_[0], $_[2].…. statements Return value – } – After all the subs are defined they are then called as required: my $variable = subname($name); The SubroutineExample2.pl

4 Returning values A basic return uses the “return $var1” / “return @sequences”…. The values can also be returned if they are the last statement in the program. – Sub-routine biggest{ My ($a, $b, $c) = @_; # the list of “local varaible” must correspond to the types that are passed. If you pass a hash table my(%hash) = @_ use strict; use warnings; # this is for the local variable My $temp; $temp = ($a<$b ? $a : $b); #(An alternative if else statement) # returning the value (last statement in the function) if ($temp < $c ) – { &temp} # this is returned Else – {$c} #this is returned – } – In main – print “The biggest of the three number is: biggest(5,2,4)”

5 Passing by reference By using the \ in a function call you pass the pointer to the subroutine The pointer can be de-referenced by using the $ value – sub ByReference (PassReference.pl) – { – print " In sub5: parms = @_ \n"; – my($val1) = $_[0]; – $arry_ptr = $_[1]; – $var2_ptr = $_[2]; – print " In sub5: \$val1 = ", $val1, " Address = ", \$val1, "\n"; – $val1 = $val1 - 1; – print " In sub5: Var1 = ", $val1, "\n"; – print " In sub5: Var2 = ", $var2_ptr, " Contents = ", $$var2_ptr, "\n"; – $$var2_ptr = $$var2_ptr - 1; – print " In sub5: Variable1 = ", $var2_ptr, " Contents = ", $$var2_ptr, "\n"; – chop(@$arry_ptr); – print " In sub5: Array1 = ", $arry_ptr, " Contents = ", @$arry_ptr, "\n"; – } – In main – ByReference($var1, \@arr1, \$var2); (what are the values of output)

6 Exercises Write a program using subroutines that: 1.Confirms if the user has input the code in the following format: Classcode_yearcode(papercode) E.g dt249 4(w203c) 5.Modify the sequence size example (from “basic pattern matching” lecture) – Allow the user to input a file name and determine its length. – Write the script using one or more subroutines.

7 Exercises 3.Write a sub-routine that can find the reverse complement of an DNA sequence. Use this subroutine to print out the compliment of each sequence line of a DNA fasta file 4.write a subroutine to convert a DNA sequence to amino acid sequence; use this to translated all three reading frames in a fasta file.


Download ppt "Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return."

Similar presentations


Ads by Google