subroutines and references

Slides:



Advertisements
Similar presentations
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Procedural programming in Java
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Perl for biologists A. Emerson, Perl for Biologists PERL More references, complex data types and objects.
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Example 2.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Subroutines sub { –#parameters are placed – –. –return; }
PSUCS322 HM 1 Languages and Compiler Design II Parameter Passing Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Chapter 8 :: Subroutines and Control Abstraction
Apply Sub Procedures/Methods and User Defined Functions
PERL Variables and data structures Andrew Emerson, High Performance Systems, CINECA.
Computer Programming for Biologists Class 8 Nov 28 th, 2014 Karsten Hokamp
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CPS120: Introduction to Computer Science Functions.
Introduction to Perl Yupu Liang cbio at MSKCC
ISBN Chapter 8 Statement-Level Control Structures.
CPS120: Introduction to Computer Science Lecture 14 Functions.
David Stotts Computer Science Department UNC Chapel Hill.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Procedural programming in Java Methods, parameters and return values.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
I Power Higher Computing Software Development High Level Language Constructs.
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Chapter 8: Advanced Method Concepts. Understanding Parameter Types Mandatory parameter – An argument for it is required in every method call Four types.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Information and Computer Sciences University of Hawaii, Manoa
Introduction to Perl: Part II
Chapter 7: User-Defined Functions II
Def: A control structure is a control statement and
Learning to Program D is for Digital.
METHODS AND BEHAVIORS AKEEL AHMED.
Subroutines Web Programming.
CSC 533: Programming Languages Spring 2015
Object-Oriented Programming Using C++ Second Edition
CSC 533: Programming Languages Spring 2018
ITM 352 Functions.
CSC 533: Programming Languages Spring 2019
Presentation transcript:

subroutines and references PERL subroutines and references Andrew Emerson, High Performance Systems, CINECA

Consider the following code: # Counts Gs in various bits of DNA $dna=“CGGTAATTCCTGCA”; $G_count=0; for ($pos=0; $pos <length $dna; $pos++) { $base=substr($dna,$pos,1); ++$G_count if ($base eq ‘G’); } # end for . . .do something else $new_dna = <DNA_FILE>; for ($pos=0; $pos <length $new_dna; $pos++) { $base=substr($new_dna,$pos,1);

Better if we could write something like… Inconvenient to repeat pieces of code many times if it does the same thing. Better if we could write something like… # Counts Gs in various bits of DNA # improved version (PSEUDO PERL) # Main program $dna=“CGGTAATTCCTGCA”; count_g using $dna; . . do something else $new_dna = <DNA_FILE>; count_g using $new_dna; count_G subroutine for ($pos=0;$pos<length $dna; $pos++) { $base=substr($dna,$pos,1); ++$G_count if ($base eq ‘G’); }

Subroutines The pieces of code used in this way are often called subroutines and are common to all programming languages (but with different names) subroutines (Perl, FORTRAN) functions (C,C++*,FORTRAN, Java*) procedures (PASCAL) Essential for procedural or structured programming. * object-oriented programming languages

Advantages of using subroutines Saves typing → fewer lines of code →less likely to make a mistake re-usable if subroutine needs to be modified, can be changed in only one place other programs can use the same subroutine can be tested separately makes the overall structure of the program clearer

Program design using subroutines Conceptual flow subroutines can use other subroutines to make more complex and flexible programs

Program design using subroutines -pseudo code # # Main program # pseudo-code ..set variables . call sub1 call sub2 call sub3 exit program sub 1 # code for sub 1 exit subroutine sub 2 # code for sub 1 exit subroutine sub 4 # code sub4 exit sub 3 # code for sub 1 call sub 4 exit subroutine

Using subroutines in Perl Example 1. # Program to count Gs in DNA sequences # (valid perl) # Main program $dna=“GGCCTAACCTCCGGT”; count_G; print “no. of G in $dna=$number_of_g\n”; # subroutines sub count_G { for ($pos=0;$pos<length $dna; $pos++) { $base=substr($dna,$pos,1); ++$number_of_g if ($base eq ‘G’); } # end for }

Subroutines in Perl Defined using the sub command: sub name { ... } Called from the main program or another subroutine using its name: name; Sometimes you will see in old Perl programs (like mine) &name; But is optional in modern Perl.

Subroutines in Perl Subroutines can be placed anywhere in the program but best to group them at the end; # main program . exit; # subroutines defined here sub sub1 { ... } sub sub2 { sub sub3 { exit not strictly necessary, but makes it clear we want to leave the program here.

Return to example 1- why is this bad? # Program to count Gs in DNA sequences # Main program $dna=“GGCCTAACCTCCGGT”; count_G; print “no. of G in $dna=$number_of_g\n”; exit; # subroutines sub count_G { for ($pos=0;$pos<length $dna; $pos++) { $base=substr($dna,$pos,1); ++$number_of_g if ($base eq ‘G’); } # end for } What does count_G need ? Where did this come from?

Perl subroutines-passing parameters The input/outputs to a subroutine are specified using parameters (or arguments) given when the subroutine is called: $no_of_G = count_g($dna); It is now clear what the subroutine expects as input and what is returned as output. Other examples: $day_in_yr = calc_day($day,$month); $num_sequences = read_sequence_file(@database);

Input parameters All the parameters in a Perl subroutine (including arrays) end up in a single array called @_ Therefore in the code: # $pos = find_motif($motif,$protein); . sub find_motif { $a = $_[0]; $b = $_[1]; ... } $a takes the value of $motif $b takes the value of $protein

Subroutine output (return values) A subroutine does not have to explicitly return something to the main program: print_title; sub print_title{ print “Sequence Manipulation program\n”; print “-----------------------------\n”; print “Written by: A.Nother \n”; print “Version 1.1: \n” } but often it does, even if only to signal the procedure went well or gave an error.

Subroutine return values By default the subroutine returns the last thing evaluated but you can use the return statement to make this explicit: input sub count_G { $dna=@_[0]; for ($pos=0;$pos<length $dna; $pos++) { $base=substr($dna,$pos,1); ++$number_of_g if ($base eq ‘G’); } # end for return $number_of_g; } output return also exits the subroutine

Return Values You can return from more than 1 point in the sub – can be useful for signalling errors: if ($dna eq “”) { return “No DNA was given”; # exit with error message } else { .. } # end if return $number_of_G; } # end sub In which case the calling program or sub should check the return value before continuing. However, in general best to return from only 1 point, otherwise difficult to follow the logic.

Return values Can also return multiple scalars, arrays, etc. but just as for the input everything ends up in a single array or list: @DNA = read_file($filename); . ($nG,$nC,$nT,$nA) = count_bases($dna); sub count_bases { ... return ($num_G,$num_C,$num_T,$num_A); } # end sub note ( and ) for the list

Counting bases – Attempt 2 # Program to count Gs in DNA sequences # using input/output parameters # Main program $dna=“GGCCTAACCTCCGGT”; $num_g = count_G($dna); print “no. of G in $dna=$num_g\n”; exit; # subroutines sub count_G { $dna=$_[0]; for ($pos=0;$pos<length $dna; $pos++) { $base=substr($dna,$pos,1); ++$number_of_g if ($base eq ‘G’); } # end for return $number_of_g; # return value of sub } better but ... All the variables inside are also visible outside the sub, not only params !

The need for variable scoping A subroutine written this way is in danger of overwriting a variable used elsewhere in the program. Remember that a subroutine should work like a black box, apart from well-defined inputs/outputs it should not affect the rest of the program. input Apart from input/output all vars needed by the sub should appear and disappear within the sub. Allows us also to use the same names for vars outside and inside the sub without conflict. sub output

Variable scoping in Perl By default, all variables defined outside a sub are visible within it and vice-versa – all variables are global. Therefore the sub can change variables used outside the sub. Solution ? Restrict the scope of the variables by making them local to the subroutine → eliminate the risk of altering a variable present outside the sub. Also makes it clear what the subroutine needs to function. scope = l’ambito ? Remember, In addition modifying $_[0], $_[1], ... within the sub will change the original variables .

Variable scoping in Perl In Perl, variables are made local to a subroutine (or a block) using the my keyword. For example, my variable1; # simple declaration my $dna=“GGTTCACCACCTG”; # with initialization my ($seq1,$seq2,$seq3); # more than 1 Attenzione my $seq1, $seq2; This means my $seq1; $seq2; Which is valid Perl so the compiler won’t give an error. Must use () if multiple vars per line how do you say initialise = inizializzare, settare

Subroutines with local variables # Program to count Gs in DNA sequences – final version # Main program $dna=“GGCCTAACCTCCGGT”; $num_g = count_G($dna); print “no. of G in $dna=$num_g\n”; exit; # subroutines sub count_G { my $dna=$_[0]; my ($pos,$base); my $number_of_g=0; for ($pos=0;$pos<length $dna; $pos++) { $base=substr($dna,$pos,1); ++$number_of_g if ($base eq ‘G’); } # end for return $number_of_g; # return value } Remember that my makes a copy of the variable. Variables declared like this is called lexical scoping in the Perl man pages

Other examples of subroutine use sub find_motif { my $motif=shift; # shifts the @_ array my $protein=shift; # (avoids $_[0], etc) ... } sub count_C_and_G { my @fasta_lines=@_; return ($num_C,$num_G); # returns a list sub reverse_seq { use strict; # the strict command enforces use of my $seq=$_[0]; # so this line will give an error (even # if defined in main program)

Question: What if we want to pass two arrays ? $seq=compare_seqs(@seqs1,@seqs2); Remember that everything arrives in the sub in a single array. Likewise for return values: (@annotations,@dna) = parse_genbank(@dna); ... sub parse_genbank { return (@annotations,@dna); } In the first example what will be in the special array @_ ? Solution: Use references

subroutines – call by value Consider $i=2; $j =add_100($i); print “i=$i\n”; sub simple_sub { my $i=$_[0]; $i=$i+100; return $i; } $i unaffected by the subroutine This is called Call by Value because a copy is made of the parameter passed and whatever happens to this copy in the subroutine doesn’t affect the variable in the main program

What are references? References can be considered to be an identifier or some other description of the objects rather than the objects themselves. reference To buy Bananas Beer Fruit Pasta Frozen pizza more beer or copy

References In computing, references are often addresses of objects (scalars,arrays,..) in memory: @genbank $dna 100 200 300 address of $dna scalar address of @genbank array

References References (sometimes called pointers in other languages) can be more convenient because in Perl they are scalars → often much smaller than the object they refer to (e.g. an array or hash). Array references can be passed around and copied very efficiently, often also using less memory. Being scalars, they can be used to make complicated data structures such as arrays of arrays, arrays of hashes and so on..

References in Perl Simplest way to create a reference in Perl is with \ $scalar_ref = \$sequence; # reference to a scalar $dna_ref = \@DNA_list; # reference to an array $hash_ref = \%genetic_code; # reference to a hash To get back the original object the reference needs to be dereferenced; $scalar = $$scalar_ref; # for scalars just add $ @new_dna = @$dna_ref; # for arrays just add @ %codon_lookup = %$hash_ref; # similary for hashes

Passing two arrays into a sub using references # compare two databases, each held as an array # $results = compare_dbase(\@dbase1,\@dbase2); # supply refs ... sub compare_dbase { my ($db1_ref,$db2_ref) = @_; # params are refs to arrays @db1 = @$db1_ref; # dereference @db2 = @$db2_ref; # dereference ... # now use @db1,@db2 return $results; } Similarly we can return 2 or more arrays by the same method

References – final words Caution: Calling by reference can change the original variables; OUTPUT dna1=G G T C T G A A A A A dna2=A A A A A @dna1=(G,G,T,C,T,G); @dna2=(A,A,A,A,A); add_seqs(\@dna1,\@dna2); print “dna1=@dna1 \n dna2=@dna2 \n”; sub add_seqs { my ($seq1,$seq2) =@_; push(@$seq1,$@seq2); } If you don’t want this behaviour then create local copies of the arrays as in previous example.

subroutines-summary subroutines defined with sub represent the main tool for structuring programs in Perl. variables used only by the subroutine should be declared with my, to prevent conflict with external variables (lexical scoping) parameters passed in to the sub end up in the single array @_; similarly for any return values array references need to be used to pass two or more arrays in (call by reference) or out of a sub.