0% found this document useful (0 votes)
7 views

pract 4

The document outlines the concept of subroutines in Perl programming, explaining their definition, advantages, and disadvantages. It provides examples of how to create and call subroutines, including passing parameters for calculations like area and perimeter of a rectangle. The conclusion emphasizes the benefits of using subroutines for code reusability and modularity.

Uploaded by

Aditya Khabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

pract 4

The document outlines the concept of subroutines in Perl programming, explaining their definition, advantages, and disadvantages. It provides examples of how to create and call subroutines, including passing parameters for calculations like area and perimeter of a rectangle. The conclusion emphasizes the benefits of using subroutines for code reusability and modularity.

Uploaded by

Aditya Khabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DATE: / /2025

PRACTICAL NO 4
Subroutine
AIM : To perform a Subroutine function in Perl programming.
INTRODUCTION: A subroutine is a block of code that can be reusable across programs. The
Perl subroutine is very flexible and powerful. In Perl, a subroutine is a group of statements that
perform a specific task. It is also known as a function or a user-defined function.Subroutines
are used to divide code into separate, reusable blocks, improving code organization and
maintainability.

A Perl function or subroutine is a group of statements that together perform a specific task. In
every programming language users want to reuse the code. So the user puts the section of code
in function or subroutine so that there will be no need to write code again and again. In Perl,
the terms function, subroutine, and method are the same but in some programming languages,
these are considered different. The word subroutines is used most in Perl programming because
it is created using keyword sub. Whenever there is a call to the function, Perl stops executing
all its program and jumps to the function to execute it and then returns back to the section of
code that it was running earlier. One can avoid using the return statement.

Defining a subroutine in Perl is straightforward. The sub keyword begins the definition,
followed by the subroutine name and a block of code enclosed in curly braces {}.Once a
subroutine is defined, calling or invoking it is as easy as writing the name of the subroutine
followed by parentheses ().

For passing the parameter:

Subroutines can also accept parameters, providing flexibility in manipulating data. In Perl,
arguments passed to a subroutine are stored in a special array @_(it is used for any parameter
like array,scalar and hashes).

Defining Subroutines:

The general form of defining the subroutine in Perl is as follows- sub subroutine_name

{
# body of method or subroutine;

37
Calling Subroutines:

In Perl subroutines can be called by passing the arguments list to it as follows-


subroutine_name(aruguments_list);

Advantages of Perl Subroutines:


1. Code Reusability:

Subroutines allow you to encapsulate reusable blocks of code, making your program modular
and reducing redundancy. You can call the same subroutine from multiple places in the program

without rewriting the logic.

2. Encapsulation:

By grouping related code into subroutines, you can hide the complexity of the implementation.
This helps in organizing the code better and makes maintenance easier.

3. Easier Debugging:
Subroutines provide a way to isolate sections of code, making it easier to test and debug. If
there's an issue, you can focus on individual subroutines rather than the entire program.
4. Parameter Passing:

You can pass data to a subroutine, allowing it to work with different inputs. Perl uses the @_
array to access these passed arguments.

Disadvantages of Perl Subroutines:


1. Overhead of Function Calls:

Calling a subroutine incurs some runtime overhead due to argument passing, stack
manipulation, and returning values. In performance-critical applications, this can add
unnecessary overhead.

2. No Strict Typing:
Perl is not strongly typed, which can lead to potential issues if incorrect data types are passed
to a subroutine. For example, passing an array where a scalar is expected might not throw an
error immediately, leading to subtle bugs.

3. Limited Parameter Validation:

Perl doesn't enforce parameter validation automatically, so it's up to the programmer to check
the passed arguments. This can lead to bugs if the subroutine is used incorrectly without proper
validation.
4. Difficulty with Return Values:

38
Subroutines return the last evaluated expression, but if you need to return multiple values, you
have to use structures like arrays or hashes. This can make subroutine return behavior less
intuitive compared to languages with explicit multiple return values.

Q1. Write a Perl program to Create a subroutine named Calculate and find area and
perimeter a rectangle

CODE:

sub calculate{

print"Enter the length = ";

$length = <stdin>;

print"Enter the breadth = ";

$breadth = <stdin>;
$area=$length*$breadth;

$perimeter=2*($length+$breadth);

print"\n area of rectangle is = $area sq.m ";

print"\n perimeter of rectangle is = $perimeter ";

calculate();

OUTPUT:

Fig 1. Display a script using subroutine function to calculate area and perimeter of rectangle

Q2. Write a Perl program to Create a subroutine named Calculate and find area and
perimeter a rectangle with parameter.

CODE:
sub cal

{
39
($l, $b) = @_;

@area = $l*$b;

print "Area of rectangle is: @area \n";

@perimeter = 2*($l+$b);
print "Perimeter of rectangle is: @perimeter \n";

print "Enter length and breadth: \n";

$l = <stdin>;

$b = <stdin>;

cal($l,$b);

OUTPUT:

Fig 2. Display a script using subroutine function to calculate area and perimeter of rectangle
using parameter
Result: Subroutine programs in Perl were successfully implemented both with parameters and
without parameters. The execution demonstrated the flexibility of subroutines in handling
different input scenarios, enhancing code reusability and modularity.

Conclusion: The implementation of Perl subroutine programs provided insight into the use of
functions with and without parameters. This approach facilitates structured programming by
reducing redundancy and improving code efficiency.

References:

1. GeeksforGeeks. (2018, July 13). Perl | Subroutines or functions. GeeksforGeeks.


https://www.geeksforgeeks.org/perl-subroutines-or-functions/

2. Javatpoint. (n.d.). Perl Functions and Subroutines. Javatpoint.


https://www.javatpoint.com/perl-functions-and-subroutines

3. Perl Tutorial. (2023, February 9). Perl subroutine. https://www.perltutorial.org/perl-


subroutine/

40

You might also like