SlideShare a Scribd company logo
2
Most read
4
Most read
6
Most read
SUBROUTINES
SUBROUTINES
• Thus far we have utilized Perl predefined functions such as print and chomp. However, it is
useful to be able to create our own functions to simplify tasks.
• A subroutine is a user defined function that allows the scripter to:
• Organize code
• Recycle code
• Reuse code
• Recall the scope of a function is local; Therefore we must take that into account when
developing them if we want global actions.
• Good Programming: Place subroutines below your main script
DEFINING A SUBROUTINE
• Perl has simplified the task to define a
function
• Question: In other languages how do we
define a function/method?
• Code: sub subName
{
Block;
}
• Common Mistakes: Thinking that
subroutines are like functions and placing ()
at the end of the definition.
• Ex.
use Cwd;
use strict;
my $currPath = getcwd;
sub homeSwitch
{
if ($currPath ne “/u/j/s/jsilver5/”)
{
$currPath = getcwd;
chdir “/u/j/s/jsilver5/”;
}
else
{
chdir $currPath;
}
}
INVOKING/CALLING A SUBROUTINE
• In order to call a subroutine use an & followed by the name of the subroutine.
• Code: &subName;
• Ex.
• &homeSwitch;
• Common Mistakes: Thinking that subroutines are like functions and placing ()
at the end of the call
MAKE IT WORTHWHILE WITH A RETURN
• Each subroutine calculates a return value upon the completion of the subroutines execution.
• More often than not you will want to store the outcome of the subroutine to be used later in
the script, repeatedly, etc.
• Larry thought it was wasteful and required too much effort to determine a difference
between void and data type functions that he made all Perl functions return a value.
• Specifically the last calculation performed in a subroutine is stored as the return of the
subroutine.
RETURN EXAMPLES
Question: What is the return value for each of the following?
• Ex 2
my $birthStone = “Diamond”;
my $street = “Patriot”;
sub nameSmush
{
birthStone = $birthstone . “ the “ . $street;
}
print(&nameSmush);
• Ex 3
my $birthStone = “Diamond”;
my $street = “Patriot”;
sub nameSmush
{
$birthStone = $birthstone . “ the “ . $street;
print(“Your superhero name is $birthStone“);
}
$testVar = &nameSmush;
• Ex 1.
my $birthStone = “Diamond”;
my $street = “Patriot”;
sub nameSmush
{
birthStone = $birthstone . “ the “ .
$street;
}
&nameSmush;

More Related Content

PPT
Data transfer and manipulation
PDF
Embedded C - Lecture 2
PPTX
C Programming Unit-1
PPTX
Array Of Pointers
PDF
Embedded C - Lecture 4
ODP
OpenGurukul : Language : C Programming
PDF
Function in C
PDF
Displacement addressing
Data transfer and manipulation
Embedded C - Lecture 2
C Programming Unit-1
Array Of Pointers
Embedded C - Lecture 4
OpenGurukul : Language : C Programming
Function in C
Displacement addressing

What's hot (20)

PPTX
Interrupts on 8086 microprocessor by vijay kumar.k
PPTX
Register presentation
PDF
Class and object
PPT
6 multiprogramming & time sharing
PPT
Operators in C++
PPTX
Computer architecture control unit
PPTX
Union in C programming
PPTX
Micro programmed control
PDF
Inter process communication
PPTX
User defined functions in C
PPTX
Type casting in c programming
PPT
Memory allocation in c
PPTX
Encapsulation
PDF
Memory management
PPTX
Object Oriented Programming Using C++
PPTX
Types of loops in c language
PPTX
Pointers in c language
PPTX
Multiprocessor
PPTX
C Programming: Control Structure
Interrupts on 8086 microprocessor by vijay kumar.k
Register presentation
Class and object
6 multiprogramming & time sharing
Operators in C++
Computer architecture control unit
Union in C programming
Micro programmed control
Inter process communication
User defined functions in C
Type casting in c programming
Memory allocation in c
Encapsulation
Memory management
Object Oriented Programming Using C++
Types of loops in c language
Pointers in c language
Multiprocessor
C Programming: Control Structure
Ad

Viewers also liked (19)

PPTX
Passing Arguments
PPT
Stack and subroutine
PPTX
Processing with Regular Expressions
PPTX
File I/O
PPTX
Matching with Regular Expressions
PPTX
More Pattern Matching With RegEx
PPTX
More Perl Basics
PPTX
Regular Expressions
PPTX
CSV File Manipulation
PPTX
Processing Regex Python
PPTX
Reading and Writing Files
PPTX
Python Basics
PPTX
PPTX
Stacks & subroutines 1
PPTX
Computer organization
PPTX
Basic Computer Organization and Design
PPT
Ntroduction to computer architecture and organization
PPTX
Computer architecture and organization
PPT
basics of computer system ppt
Passing Arguments
Stack and subroutine
Processing with Regular Expressions
File I/O
Matching with Regular Expressions
More Pattern Matching With RegEx
More Perl Basics
Regular Expressions
CSV File Manipulation
Processing Regex Python
Reading and Writing Files
Python Basics
Stacks & subroutines 1
Computer organization
Basic Computer Organization and Design
Ntroduction to computer architecture and organization
Computer architecture and organization
basics of computer system ppt
Ad

Similar to Subroutines (20)

PPTX
Unit 1-subroutines in perl
PPTX
Subroutines in perl
PDF
Marc’s (bio)perl course
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
PDF
PPT
SD & D modularity
PDF
Perl_Part5
PDF
Lazy Data Using Perl
PPT
Plunging Into Perl While Avoiding the Deep End (mostly)
PPT
Perl Intro 7 Subroutines
PDF
PDF
Working Effectively With Legacy Perl Code
PDF
Perl intro
PDF
Best practices naming conventions
PDF
Scripting3
PPT
Introduction to Perl
PDF
PerlIntro
PDF
PerlIntro
ODP
WTFin Perl
Unit 1-subroutines in perl
Subroutines in perl
Marc’s (bio)perl course
You Can Do It! Start Using Perl to Handle Your Voyager Needs
SD & D modularity
Perl_Part5
Lazy Data Using Perl
Plunging Into Perl While Avoiding the Deep End (mostly)
Perl Intro 7 Subroutines
Working Effectively With Legacy Perl Code
Perl intro
Best practices naming conventions
Scripting3
Introduction to Perl
PerlIntro
PerlIntro
WTFin Perl

More from primeteacher32 (20)

PPT
Software Development Life Cycle
PPTX
Variable Scope
PPTX
Returning Data
PPTX
Intro to Functions
PPTX
Introduction to GUIs with guizero
PPTX
Function Parameters
PPTX
Nested Loops
PPT
Conditional Loops
PPTX
Introduction to Repetition Structures
PPTX
Input Validation
PPTX
Windows File Systems
PPTX
Nesting Conditionals
PPTX
Conditionals
PPT
Intro to Python with GPIO
PPTX
Variables and Statements
PPTX
Variables and User Input
PPT
Intro to Python
PPTX
Raspberry Pi
PPT
Hardware vs. Software Presentations
PPTX
Block chain security
Software Development Life Cycle
Variable Scope
Returning Data
Intro to Functions
Introduction to GUIs with guizero
Function Parameters
Nested Loops
Conditional Loops
Introduction to Repetition Structures
Input Validation
Windows File Systems
Nesting Conditionals
Conditionals
Intro to Python with GPIO
Variables and Statements
Variables and User Input
Intro to Python
Raspberry Pi
Hardware vs. Software Presentations
Block chain security

Recently uploaded (20)

PPTX
Your Guide to a Winning Interview Aug 2025.
PPTX
Prokaryotes v Eukaryotes PowerPoint.pptx
PDF
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf
PPTX
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
PPTX
The Stock at arrangement the stock and product.pptx
PDF
Shopify Store Management_ Complete Guide to E-commerce Success.pdf
PPTX
Principles of Inheritance and variation class 12.pptx
PDF
Parts of Speech Quiz Presentation in Orange Blue Illustrative Style.pdf.pdf
PPTX
Definition and Relation of Food Science( Lecture1).pptx
PPTX
The-Scope-of-Food-Quality-and-Safety.pptx managemement
PPT
2- CELL INJURY L1 Medical (2) gggggggggg
PDF
Blue-Modern-Elegant-Presentation (1).pdf
PPTX
microtomy kkk. presenting to cryst in gl
PPTX
_Dispute Resolution_July 2022.pptxmhhghhhh
PDF
Beginner’s Guide to Digital Marketing.pdf
PPTX
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
PDF
Sheri Ann Lowe Compliance Strategist Resume
PPTX
GPAT Presentation PPT and details about imp topics.pptx
PPT
Gsisgdkddkvdgjsjdvdbdbdbdghjkhgcvvkkfcxxfg
PDF
APNCET2025RESULT Result Result 2025 2025
Your Guide to a Winning Interview Aug 2025.
Prokaryotes v Eukaryotes PowerPoint.pptx
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
The Stock at arrangement the stock and product.pptx
Shopify Store Management_ Complete Guide to E-commerce Success.pdf
Principles of Inheritance and variation class 12.pptx
Parts of Speech Quiz Presentation in Orange Blue Illustrative Style.pdf.pdf
Definition and Relation of Food Science( Lecture1).pptx
The-Scope-of-Food-Quality-and-Safety.pptx managemement
2- CELL INJURY L1 Medical (2) gggggggggg
Blue-Modern-Elegant-Presentation (1).pdf
microtomy kkk. presenting to cryst in gl
_Dispute Resolution_July 2022.pptxmhhghhhh
Beginner’s Guide to Digital Marketing.pdf
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
Sheri Ann Lowe Compliance Strategist Resume
GPAT Presentation PPT and details about imp topics.pptx
Gsisgdkddkvdgjsjdvdbdbdbdghjkhgcvvkkfcxxfg
APNCET2025RESULT Result Result 2025 2025

Subroutines

  • 2. SUBROUTINES • Thus far we have utilized Perl predefined functions such as print and chomp. However, it is useful to be able to create our own functions to simplify tasks. • A subroutine is a user defined function that allows the scripter to: • Organize code • Recycle code • Reuse code • Recall the scope of a function is local; Therefore we must take that into account when developing them if we want global actions. • Good Programming: Place subroutines below your main script
  • 3. DEFINING A SUBROUTINE • Perl has simplified the task to define a function • Question: In other languages how do we define a function/method? • Code: sub subName { Block; } • Common Mistakes: Thinking that subroutines are like functions and placing () at the end of the definition. • Ex. use Cwd; use strict; my $currPath = getcwd; sub homeSwitch { if ($currPath ne “/u/j/s/jsilver5/”) { $currPath = getcwd; chdir “/u/j/s/jsilver5/”; } else { chdir $currPath; } }
  • 4. INVOKING/CALLING A SUBROUTINE • In order to call a subroutine use an & followed by the name of the subroutine. • Code: &subName; • Ex. • &homeSwitch; • Common Mistakes: Thinking that subroutines are like functions and placing () at the end of the call
  • 5. MAKE IT WORTHWHILE WITH A RETURN • Each subroutine calculates a return value upon the completion of the subroutines execution. • More often than not you will want to store the outcome of the subroutine to be used later in the script, repeatedly, etc. • Larry thought it was wasteful and required too much effort to determine a difference between void and data type functions that he made all Perl functions return a value. • Specifically the last calculation performed in a subroutine is stored as the return of the subroutine.
  • 6. RETURN EXAMPLES Question: What is the return value for each of the following? • Ex 2 my $birthStone = “Diamond”; my $street = “Patriot”; sub nameSmush { birthStone = $birthstone . “ the “ . $street; } print(&nameSmush); • Ex 3 my $birthStone = “Diamond”; my $street = “Patriot”; sub nameSmush { $birthStone = $birthstone . “ the “ . $street; print(“Your superhero name is $birthStone“); } $testVar = &nameSmush; • Ex 1. my $birthStone = “Diamond”; my $street = “Patriot”; sub nameSmush { birthStone = $birthstone . “ the “ . $street; } &nameSmush;