Subroutines. Subroutines. The Basics. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc.

Similar documents
CS 3101 _ programming languages (perl)

Modularity and Reusability I. Functions and code reuse

CS 105 Perl: Perl subroutines and Disciplined Perl

Control Structures. Important Semantic Difference

CS 105 Perl: Completing the Toolbox

CS 105 Perl: Modules and Objects

@EXPORT_OK = qw(munge frobnicate); # symbols to export on request

package YourModule; require = = qw(munge frobnicate); # symbols to export on request

use Params::Check qw[check allow last_error];

IT441. Subroutines. (a.k.a., Functions, Methods, etc.) DRAFT. Network Services Administration

$bool = $obj->mk_aliases( # create an alias to an existing alias_name => 'method'); # method name

Intro. Scheme Basics. scm> 5 5. scm>

Subroutines in Perl. Jon-Michael Deldin. Dept. of Computer Science University of Montana September 12, 2011

Fortunately, the layout is much more legible, more like BASIC's PRINT USING statement. Think of it as a poor man's nroff(1). nroff

IT441. Network Services Administration. Data Structures: Arrays

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

Classnote for COMS6100

COMS 3101 Programming Languages: Perl. Lecture 6

COMS 3101 Programming Languages: Perl. Lecture 2

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

perl -MO=Deparse[,-d][,-fFILE][,-p][,-q][,-l] [,-sletters][,-xlevel] prog.pl

perl -MO=Deparse[,-d][,-fFILE][,-p][,-q][,-l] [,-sletters][,-xlevel] prog.pl

This section provides some reminders and some terminology with which you might not be familiar.

The Eobj Perl environment

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Lexical Considerations

1 Apache2::Filter - Perl API for Apache 2.0 Filtering

COMS 3101 Programming Languages: Perl. Lecture 5

Regular expressions and case insensitivity

Tieing and Overloading Objects in Perl. Dave Cross Magnum Solutions

Final thoughts on functions F E B 2 5 T H

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Perl Library Functions

Lexical Considerations

Wednesday, October 15, 14. Functions

Pathologically Eclectic Rubbish Lister

6.096 Introduction to C++

threads::shared - Perl extension for sharing data structures between threads

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Scripting Languages Perl Basics. Course: Hebrew University

Function Call Stack and Activation Records

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

More Perl. CS174 Chris Pollett Oct 25, 2006.

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

Ruby: Introduction, Basics

# use a BEGIN block so we print our plan before MyModule is loaded BEGIN { plan tests => 14, todo => [3,4] }

1 Lexical Considerations

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Implementing Functions at the Machine Level

Regular expressions and case insensitivity

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

This document describes version 0.87 of Attribute::Handlers, released September 21, 2009.

MIDTERM EXAM (Solutions)

Using References to Create Complex Structures. The array and hash composers

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

G Programming Languages - Fall 2012

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

QUIZ. What is wrong with this code that uses default arguments?

Introduction to Perl Session 6. special variables subroutines Introduction to Perl

CS240: Programming in C

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

A control expression must evaluate to a value that can be interpreted as true or false.

Final CSE 131B Spring 2004

They grow as needed, and may be made to shrink. Officially, a Perl array is a variable whose value is a list.

C++ Reference NYU Digital Electronics Lab Fall 2016

More Scripting Techniques Scripting Process Example Script

CS-201 Introduction to Programming with Java

pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap

Scope: Global and Local. Concept of Scope of Variable

Sixth lecture; classes, objects, reference operator.

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

Time : 1 Hour Max Marks : 30

CS 360: Programming Languages Lecture 12: More Haskell

CS558 Programming Languages

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Learning Perl 6. brian d foy, Version 0.6, Nordic Perl Workshop 2007

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

SPIM Procedure Calls

Chapter 1 Getting Started

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

CPSC 427a: Object-Oriented Programming

A Crash Course in Perl5

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Programming Languages

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

use attributes (); # optional, to get subroutine declarations = attributes::get(\&foo);

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

Data Structure Layout. In HERA/Assembly

Chapter 6: Functions

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Transcription:

Subroutines Subroutines aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We ll just say Subroutines. "Functions" generally means built-in functions perldoc perlsub The Basics sub myfunc { print "Hey, I m in a function!\n"; # myfunc(); Because the subroutine is already declared, () are optional (ie, you can just say myfunc; ) If you call the function before declaring it, the () are required You can declare a subroutine without defining it (yet): sub myfunc; Make sure you define it eventually. actual name of the subroutine is &myfunc ampersand not necessary to call it in fact, has (usually undesirable) side-effects 1

Parameters (aka Arguments, inputs, etc) You can call any subroutine with any number of parameters. The parameters get passed in via local @_ variable. my $foobar = 82; myfunc('hello', 'world', $foobar); sub myfunc{ print join(' ', @_), "\n"; @_ is a normal array in every way. In this subroutine, $_[0] = 'hello', $_[1] = 'world', and $_[2] = 82 prints 'hello world 82 ' Standard Procedure There are two "normal" ways to obtain individual parameters: sub display { my ($name, $addr) = @_; #... shift() in a subroutine acts on @_ with no args Outside of a subroutine, acts on @ARGV sub display { my $name = shift; my $addr = shift; #... Beware that the second method destroys @_ in the process. Pass by value vs Pass by reference *All* parameters are passed by reference. A direct change to an element of @_ will affect the variable passed in. To pass by value instead, create a copy: my ($foo, $bar) = ('old', 'old'); change($foo, $bar); sub change { my ($val1, $val2) = @_; $_[0] = 'new'; #changes $foo $val2 = 'new'; #does not change $bar $foo 'new', $bar 'old' If you use the shift() method, you lose the ability to change the parameters! 2

& side effect #1 If you use & to call a subroutine, and don't pass any arguments, the current value of @_ will be passed automatically. &myfunc; myfunc s @_ is alias to current @_ same as saying myfunc(@_); but faster internally In general, don't call the subroutine with &. if your subroutine checks for parameters, and you don't explicitly pass parameters, @_ will not be empty as you expect. Squashing array parameters If arrays or hashes are passed into a subroutine, they get squashed into one flat array: @_ my @a = (1, 2, 3); my @b = (8, 9, 10); myfunc (@a, @b); inside myfunc, @_ (1, 2, 3, 8, 9, 10); Same as my @c = (@a, @b); myfunc(@c); Maybe this is what you want. if not, you need to use references References in Parameters To pass arrays (or hashes), and not squash them: my @a = (1, 2, 3); my @b = (8, 9, 10); myfunc (\@a, \@b); In subroutine, @_ contains two scalar values. Each one is a reference to an array. sub myfunc{ my ($ref1, $ref2) = @_; my @x = @{$ref1; my @y = @{$ref2; # 3

Pass by Reference, take 2 To not change a scalar, copy the value from @_ foo($val); sub foo { $_[0] = 'new'; Changes $val sub foo { my $copy = $_[0]; $copy = 'new'; Does not change $val To not change an array, you must copy the array that is referenced! bar(\@vals); sub bar { push @{$_[0], 'new'; Changes @vals sub bar { my $copy = $_[0]; push @{$copy, 'new'; CHANGES @vals!!! $_[0] and $copy are two different references, but they both refer to the same array sub bar { my @copy = @{$_[0]; push @copy, 'new'; Does not change @vals @copy is a different array than @{$_[0] Return values All Perl blocks return last expression evaluated. sub add2 { $_[0] + $_[1]; $total = add2(4, 5); $total 9 return keyword used for explicitness, or to leave the subroutine before its lexical end sub myfunc{ if (!@_){ warn "myfunc called with no args!"; return -1; #... Return issues Can return values in list or scalar context. sub toupper{ my @params = @_; tr/a-z/a-z/ for @params; return @params; my @uppers = toupper $word1, $word2; my $upper = toupper $word1, $word2; $upper gets size of @params Why not use tr/a-z/a-z/ for @_;? 4

wantarray function Scalar vs List Returns Built-in function. If subroutine called in list context, wantarray returns true If subroutine called in scalar context, wantarray returns false If subroutine called in void context, wantarray returns undef. Perhaps we want to return an entire array in list context, but the first element of the array in scalar context: sub fctn{ warn "fctn() called in void context" unless defined wantarray; #... return wantarray? @params : $params[0]; Subroutine References To take a reference to a subroutine, use the & and prepend a \, like you would for any other variable: my $fooref = \&foo; You can declare a reference to an anonymous subroutine Store the return value of sub in a scalar variable $subref = sub { print "Hello\n"; ; to call, de-reference the stored value: &$subref; $subref->(); #preferred works with parameters too.. &$subref($param1, $param2); $subref->($param1, $param2); Sub refs can be stored in arrays or hashes to create "dispatch tables" my @dispatch = (\&foo, $subref, \&baz); my $var = <STDIN>; $dipatch[$var]->(); Scoping Recall that there are two distinct scopes of variables: Package variables and Lexical variables. Package variables available anywhere, without being declared ed Perl has two ways of creating 'local' variables local and my what you may think of as local (from C/C++) is actually achieved via my. local is mostly a holdover from Perl 4, which did not have lexical variables. 5

Where s the scope subroutines declared within a lexical s scope have access to that lexical this is one way of implementing static variables in Perl prior to 5.10, the only way { my $num = 20; sub add_to_num { $num++ sub print_num { print "num = $num"; add_to_num; #increments $num print_num; #prints current val of $num print $num; #ERROR! local local does not create new variable instead, assigns temporary value to existing package variable has dynamic scope functions called from within scope of local variable get the temporary value our ($x, $y) = (10, 20); sub fctn { print "x = $x, y = $y\n"; { local $x = 1; my $y = 2; fctn(); in fctn(), $main::x has a temporary value created by local The lexical $y is not accessible to fctn prints "x = 1, y = 20" state (5.10 exclusive) A lexical variable that remembers the last value it had before it went out of scope is a "static" variable use feature ':5.10'; sub count_it { state $x = 0; say "count_it called $x times before"; $x++; count_it(); count_it(); count_it(); say "I've called count_it $x times"; # ERROR!! The initialization is done only the first time this sub is called. After that, variable is initialized with last value it had before it went out of scope the last time. 6

What to know about scope my is lexically scoped Look at the actual code. Whatever block encloses my is the scope of the variable our is also lexically scoped allows you to use a global package variable without fully qualifying local is dynamically scoped The scope is the enclosing block, plus any subroutines called from within that block state t is lexically ll scoped But it remembers its previous value (only available in 5.10.0 or higher) Almost always want my instead of local notable exception: cannot create lexical variables such as $_, $/, $", $,, etc. Only normal, alpha-numeric variables Exception: can lexicalize $_ in 5.10.0 and higher. for built-in variables, localize them. See also: "Coping With Scoping" http://perl.plover.com/faqs/namespaces.html Prototypes Perl s way of letting you limit how you ll allow your sub to be called. when declaring the sub, give it the type of variables it takes sub f1($$) { f1 must take two scalar values sub f2($@) { f2 takes a scalar value, followed by a list of values recall a list can contain 0, 1, or any number of values sub f3(\@$) { f3 takes an actual array, followed by a scalar value sub f4() { f4 takes zero arguments of any kind "Too many arguments for main::f4" Get rid of those parentheses!! sub f5(_) {... f5 takes a single scalar, but defaults to $_ if not given 5.10.0 and higher only Don t use prototypes Prototypes are almost universally considered a mistake in the language. They should NEVER be used. They create a false sense of security, letting you think you don t need to check your args manually. They frequently do NOT work as expected. sub fctn($@) {... fctn(@foo); NO ERROR! Instead, converts @foo to scalar context, and lets the second argument be an empty list. sub avg($$) {... my @args = ($val1, $val2); avg(@args); ERROR! Won t let you pass the array containing two values 7

Even more pointless... The second side-effect of calling a subroutine with the & is to disable prototype checking entirely sub foo($) {... foo(@bar, %baz); #Error &foo(@bar, %baz); #No error Warning your users If something goes wrong in a subroutine, it's often helpful to know where the subroutine was called. sub fctn { warn "fctn called in void context" unless defined wantarray; This will only tell the user an error occurred within the subroutine, line number will be the line of the warn() use Carp; sub fctn { carp "fctn called in void context" unless defined wantarray; Line number reported will be line on which fctn() was called. croak : carp :: die : warn Who called me? caller EXPR - return information about what called this subroutine No args, scalar context: package from which sub was called No args, list context: (package, filename, line number) from which this sub was called Integer passed, list context: integer is how many calls to back up the call stack (0 for current sub) returns (package, filename, line number, sub name, hasargs, wantarray, evaltext...) 8