Perl Functions.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Advertisements

Programming Languages and Paradigms The C Programming Language.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Learning Ruby Methods. We’ve seen several so far: puts, gets, chomp, to_s, and even +, -, * and / Usually use dot notation is really just a shortcut.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
“For” loops. Common loop form 6 zMost common loop form: z i = start; zwhile (i < = end) z{i++;} zThere is a special form for it that reinforces the following.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
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.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Procedures and Modular Programming Part # 2. Interface Block ► Functions do not have to be internal to main program ► They can be stand-alone ► In this.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Programming Perl in UNIX Course Number : CIT 370 Week 6 Prof. Daniel Chen.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
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.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
Introduction to Programming
Programming Right from the Start with Visual Basic .NET 1/e
VB Script V B S.
Examples of Classes & Objects
Programming Languages and Paradigms
Week 8 - Friday CS 121.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Perl Reliability Workshop
Method.
Programming Paradigms
JavaScript: Functions.
Perl: Functions, References, Etc.
Chapter 14 Introduction to Ruby.
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Visual Basic .NET BASICS
Subroutines Web Programming.
Classes & Objects: Examples
Counting Loops.
Context.
Sit next to someone. Today, we do some work in pairs.
Lesson 2. Control structures File IO - reading and writing Subroutines
CSCI 431 Programming Languages Fall 2003
Coding Concepts (Basics)
Testing and Repetition
Homework Any Questions?.
Subroutines.
Programming Languages and Paradigms
Unit-2 Objects and Classes
C++ Array 1.
Basic Ruby Syntax sum = 0 i = 1 while i <= 10 do sum += i*i
Methods/Functions.
ITM 352 Functions.
Corresponds with Chapter 5
Programming II Vocabulary Review Loops & functions
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Parameters and Arguments
Presentation transcript:

Perl Functions

Defining Functions sub subroutine_name { stmt1; stmt2; … } Arguments are not listed in declaration May use a return statement If no return statement, value of last expression is returned Can return scalar, array, or hash

Calling Functions Called by sub_name(arg1, arg2, …); Parens may be omitted if declaration has already been seen Arguments are available in called function as entries in array @_

Sample Function sub max { ($a, $b) = @_; $a > $b ? $a : $b; } Puts args into variables Returns larger if they are numbers

Array and Hash Arguments Can only pass one array or hash as an argument unless you know the size Args are flattened (@a, @b) = @_; #all in @a Can precede array or hash by a fixed number of scalars but ($a, @b, $c) = @_; won’t work

Scope my($a); my $c, @e; Declares local variable with scope of block (including function) or loop (in control Parentheses are optional

Strict use strict; Directive requires Declare all variables Declare all functions before first use