Perl | Pass By Reference Last Updated : 12 Feb, 2019 Comments Improve Suggest changes Like Article Like Report When a variable is passed by reference function operates on original data in the function. Passing by reference allows the function to change the original value of a variable. When the values of the elements in the argument arrays @_ are changed, the values of the corresponding arguments will also change. This is what passing parameters by reference does. In the example given below, when we update the elements of an array @a in subroutine sample, it also changes the value of the parameters which will be reflected in the whole program. Hence when parameters are called by reference, changing their value in the function also changes their value in the main program. Example 1: perl #!/usr/bin/perl # Initialising an array 'a' @a = (0..10); # Array before subroutine call print("Values of an array before function call: = @a\n"); # calling subroutine 'sample' sample(@a); # Array after subroutine call print("Values of an array after function call: = @a"); # Subroutine to represent # Passing by Reference sub sample { $_[0] = "A"; $_[1] = "B"; } Output: Values of an array before function call: = 0 1 2 3 4 5 6 7 8 9 10 Values of an array after function call: = A B 2 3 4 5 6 7 8 9 10 Here is how the program works:- An array consisting of values from 0 to 10 is defined. Further, this array is passed to the 'sample' subroutine. A subroutine 'sample' is already defined. Inside this, the values of the first and second parameters are changed through the argument array @_. Values of the array @a are displayed after calling the subroutine. Now the first two values of the scalar are updated to A and B respectively. Example 2: perl #!/usr/bin/perl # Initializing values to scalar # variables x and y my $x = 10; my $y = 20; # Values before subroutine call print "Before calling subroutine x = $x, y = $y \n"; # Subroutine call sample($x, $y); # Values after subroutine call print "After calling subroutine x = $x, y = $y "; # Subroutine sample sub sample { $_[0] = 1; $_[1] = 2; } Output: Before calling subroutine x = 10, y = 20 After calling subroutine x = 1, y = 2 Comment More infoAdvertise with us Next Article Perl | Pass By Reference R rupanisweety Follow Improve Article Tags : Perl Perl-function Similar Reads Pass By Reference In C Passing by reference is a technique for passing parameters to a function. It is also known as call by reference, call by pointers, and pass by pointers. In this article, we will discuss this technique and how to implement it in our C program. Pass By Reference in C In this method, the address of an 4 min read Perl | References In Perl, we use variables to access data stored in a memory location(all data and functions are stored in memory). Variables are assigned with data values that are used in various operations. Perl Reference is a way to access the same data but with a different variable. A reference in Perl is a scal 4 min read C++ Functions - Pass By Reference In C++, there are different ways to pass data (or variables) to a function, with two common methods being Passing by Value and Passing by Reference. Passing by Reference lets a function modify a variable directly, without creating a copy. The variable and parameter share the same memory location, so 3 min read Perl | References to a Subroutine Prerequisite: Perl references Declaring References to a Subroutine In Perl, a reference is, exactly as the name suggests, a reference or pointer to another object. References actually provide all sorts of abilities and facilities that would not otherwise be available and can be used to create sophis 6 min read Mixed Reference in Excel A cell reference or a cell address is a way to define a cell on a worksheet, it is formed by combining a column letter and a row number. To refer any if the cell on a worksheet (in any formula/ to copy the cell) we use cell reference. Example: To refer to the cell present in row 2 & column D, ce 3 min read Like