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.
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.
Before calling subroutine x = 10, y = 20 After calling subroutine x = 1, y = 2
- How to pass integer by reference in Java
- How to pass PHP Variables by reference ?
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- How to pass form variables from one page to other page in PHP ?
- How to pass string to a component using Angular2 ?
- How to pass two parameters to EventEmitter in Angular 9 ?
- How to pass query parameters with a routerLink ?
- Passing Reference to a Pointer in C++
- Difference between Call by Value and Call by Reference
- Copy an Array by Value and Reference into Another Array in Golang
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- Why do we need reference variables if we have pointers
- Perl | split() Function
- Perl | chomp() Function
- Perl | Backtracking in Regular Expression
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.