Subroutines - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Subroutines

Description:

Because the subroutine is already declared, () are optional (ie, you ... ampersand not normally necessary to call it. Parameters (aka Arguments, inputs, etc) ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 23
Provided by: PaulL155
Category:

less

Transcript and Presenter's Notes

Title: Subroutines


1
Subroutines
2
Subroutines
  • aka user-defined functions, methods, procdures,
    sub-procedures, etc etc etc
  • Well just say Subroutines.
  • Functions generally means built-in functions
  • Well attempt to start out most basic, and work
    our way up to complicated.

3
The Basics
  • sub myfunc
  • print Hey, Im 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 normally necessary to call it

4
Parameters
  • (aka Arguments, inputs, etc)
  • You can call any subroutine with any number of
    parameters.
  • The parameters get passed in via local _at__
    variable.
  • sub myfunc
  • foreach word (_at__)
  • print word
  • foobar 82
  • myfunc hello, world, foobar
  • prints hello world 82

5
Passing current parameters
  • Can call a function with the current value of _at__
    as the parameter list by using .
  • myfunc
  • myfuncs _at__ is alias to current _at__
  • same as saying myfunc(_at__)
  • its faster internally

6
Squashing array parameters
  • If arrays or hashes passed into a subroutine,
    they get squashed into one flat array _at__
  • _at_a (1, 2, 3)
  • _at_b(8, 9, 10)
  • myfunc (_at_a, _at_b)
  • inside myfunc, _at__ ? (1, 2, 3, 8, 9, 10)
  • Maybe this is what you want.
  • if not, you need to use references

7
References in Parameters
  • To pass arrays (or hashes), and not squash them
  • sub myfunc
  • (ref1, ref2) _at__
  • _at_x _at_ref1
  • _at_y _at_ref2
  • _at_a (1, 2, 3)
  • _at_b (8, 9, 10)
  • myfunc (\_at_a, \_at_b)

8
Passing by reference
Within subroutine, changes to the array reference
passed in affect the array that was referenced
  • sub f1
  • ref1 shift(_at__)
  • ref2 shift(_at__)
  • _at_a2 _at_ref2
  • for (i0 ilt_at_ref1 i)
  • ref1i
  • for (i0 ilt_at_a2 i)
  • a2i--

_at_foo(1, 1, 1) _at_bar(1, 1, 1) f1(\_at_foo,
\_at_bar) At this point, _at_foo ? (2, 2, 2), but
_at_bar ? (1, 1, 1)
9
Return values
  • In Perl, subroutines return last expression
    evaluated.
  • sub count
  • ...
  • _0 _1
  • total count(4, 5)
  • total ? 9
  • Standard practice is to use return keyword
  • sub myfunc
  • ...
  • return retval

10
Return issues
  • Can return values in list or scalar context.
  • sub toupper
  • _at_params _at__
  • foreach (_at_params) tr/a-z/A-Z/
  • return _at_params
  • _at_uppers toupper (word1, word2)
  • upper toupper(word1, word2)
  • upper gets size of _at_params

11
Scalar vs List Returns
  • wantarray function
  • Built-in function in Perl.
  • If subroutine called in list context, wantarray
    returns a true value
  • If subroutine called in scalar context, wantarray
    returns a false value
  • If subroutine called in void context, wantarray
    returns undef.
  • Perhaps we want to return an entire list in list
    context, but the first element of the list in
    scalar context
  • sub fctn
  • return wantarray ? _at_params params0

12
Anonymous functions
  • You can declare a subroutine without giving it a
    name.
  • Store the return value of sub in a scalar
    variable
  • subref sub print Hello\n
  • to call, de-reference the stored value
  • subref
  • works with parameters too..
  • subref(param1, param2)

13
Scoping
  • Up to now, weve used global variables
    exclusively.
  • 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.

14
my
  • my creates a new variable lexically scoped to
    inner most block
  • The block may be a subroutine, loop, or bare
  • variables created with my are not accessible (or
    even visible) to anything outside scope.
  • sub fctn
  • my x shift(_at__)
  • print x ERROR!!!

15
lexical variables
  • Variables declared with my are called lexical
    variables or lexicals
  • Not only are they not visible outside block, they
    mask globals with same name
  • foo 10
  • my foo 3
  • print foo prints 3
  • print foo prints 10

16
Wheres the scope
  • subroutines declared within a lexicals scope
    have access to that lexical
  • this is one way of implementing static variables
    in Perl
  • my num 20
  • sub add_to_num num
  • sub print_num print num num\n
  • add_to_num
  • print_num
  • print num ERROR!

17
local
  • local does not create new variable
  • instead, assigns temporary value to existing
    (global) variable
  • has dynamic scope, rather than lexical
  • functions called from within scope of local
    variable get the temporary value
  • sub fctn print a a, b b\n
  • a 10 b 20
  • local a 1
  • my b 2
  • fctn()
  • prints a 1, b 20

18
What to know about scope
  • my is statically (lexically) scoped
  • Look at the actual code. Whatever block encloses
    my is the scope of the variable
  • local is dynamically scoped
  • The scope is the enclosing block, plus any
    subroutines called from within that block
  • Almost always want my instead of local
  • notable exception cannot create lexical
    variables such as _. Only normal,
    alpha-numeric variables
  • for built-in variables, localize them.

19
Prototypes
  • Perls way of letting you limit how youll allow
    your subroutine to be called.
  • when defining the function, give it the type of
    variable you want it to take
  • sub f1 ()
  • f1 must take two scalars
  • sub f2(_at_)
  • f2 takes a scalar, followed by a list
  • sub f3(\_at_)
  • f3 takes an actual array, followed by a scalar

20
Prototype conversions
  • sub fctn()
  • fctn(_at_foo, bar)
  • Perl converts _at_foo to scalar (ie, takes its
    size), and passes that into the function
  • sub fctn2(\_at_)
  • fctn2(_at_foo, bar)
  • Perl automatically creates reference to _at_foo to
    pass as first member of _at__

21
Prototype generalities
22
Getting around parameters
  • If you want to ignore parameters, call subroutine
    with character in front
  • sub myfunc (\\)
  • myfunc (_at_array) ERROR!
  • myfunc (_at_array) No error here
Write a Comment
User Comments (0)
About PowerShow.com