Broad Network


Perl Function and the return Statement

Miscellaneous Features in Perl – Part 1

Foreword: In this part of the series I talk about the function and its return statement.

By: Chrysanthus Date Published: 2 Apr 2016

Introduction

This is part 1 of my series, Miscellaneous Features in Perl. In this part of the series I talk about the function and its return statement. If you are using traditional Perl then precede each file with something like, #!/usr/bin/perl .

Pre-knowledge
At the bottom of this page, you will see links to the different series you should have read before coming here, in order to better understand this one.

The Function Structure
A function is of the form:

    sub functionName (type1 ident1, type2 ident2, …)
        {
            //statements
            return [value]; // optional
        }

The return statement is optional, and is usually the last statement, in many situations. In its absence, the result of the last statement (expression) is returned.

The return Statement
The return statement itself can simply be:

    return;

In this case nothing is returned (undef is returned).

The return statement can be:

    return value;

Here, value, can be the actual datum, e.g, a number such as, 56.

Meaning of the return Statement
The return statement means, stop executing the function just after the return statement and return to the caller (calling function). The following program illustrates this:

use strict;

sub fn()
    {
        print  1, "\n";
        print 2, "\n";
        return 56;
        print 3, "\n";
        print 4, "\n";
    }

     my $var = fn();
     print $var;

Read and try the code. Execution of the function definition, fn(), stops at the return of 56. The statements that follow in the function definition are not executed. The calling function is, “fn();”. The called function is the definition of fn().

The return Statement and the if-construct in a Function
You can decide whether or not the statements below the return statement in a function, should be executed, using the if-construct. In the next two programs, the return statement is inside the block of an if-construct in the function.

In the following program, the statements below the if-construct are executed because the if-condition is false (read and try it).

use strict;

my $flt = 2.5;

sub fn()
    {
        print  1, "\n";
        print 2, "\n";
        if ($flt == 8.7)
            {
                return 56;
            }
        print 3, "\n";
        print 4, "\n";
    }

    fn();

In the above function “fn()”, there is no effective return statement.

In the following program the statements below the if-block are not executed because the if-condition is true (read and try it):

use strict;

my $flt = 2.5;

sub fn()
    {
        print  1, "\n";
        print 2, "\n";
        if ($flt == 2.5)
            {
                return 56;
            }
        print 3, "\n";
        print 4, "\n";
    }

    fn();

Returning from outside the Function
A function does not only have to return what has been created inside the function definition. It can also return what has been created outside the function. The following program illustrates this, with the fn() function:

use strict;

my $flt = 2.5;

sub fn()
    {
        #some statements
        return $flt;
    }

my $var = fn();
print $var;

That is it for this part of the series.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

Comments

Become the Writer's Fan
Send the Writer a Message