lecture 8: basic concepts of subroutines. functions in perl functions take the following format: –...

7
Lecture 8: Basic concepts of subroutines

Upload: evan-jenkins

Post on 24-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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

Lecture 8:

Basic concepts of subroutines

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

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

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

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

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

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)”

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

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)

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

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.

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

Exercises3. 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.