Recursion in Perl Last Updated : 02 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Recursion is a mechanism when a function calls itself again and again till the required condition is met. When the function call statement is written inside the same function then such a function is referred to as a recursive function.The argument passed to a function is retrieved from the default array @_ whereas each value can be accessed by $_[0], $_[1] and so on.Example 1: The example below finds factorial of a number. Factorial of any number n is (n)*(n-1)*(n-2)*....*1. e.g.: 4! = 4*3*2*1 = 24 3! = 3*2*1 = 6 2! = 2*1 = 2 1! = 1 0! = 0 Perl #!/usr/bin/perl # Perl Program to calculate Factorial sub fact { # Retrieving the first argument # passed with function calling my $x = $_[0]; # checking if that value is 0 or 1 if ($x == 0 || $x == 1) { return 1; } # Recursively calling function with the next value # which is one less than current one else { return $x * fact($x - 1); } } # Driver Code $a = 5; # Function call and printing result after return print "Factorial of a number $a is ", fact($a); Here is how the program works : Step 1- When the value of scalar a is 0 or 1, the function will return 1 because the value of both 0! and 1! is 1. Step 2- When the value of scalar a is 2 then fac(x-1) makes a call to fac(1) and this function returns 1. So, it is 2*factorial(1) = 2*1 = 2.So, it will return 2. Step 3- Similarly when higher values are passed to function at every call argument value decreases by 1 and computes till the value reaches 1.Example 2: Example below computes the Fibonacci series till a given number. Perl #!/usr/bin/perl # Perl Program to print Fibonacci series sub fib { # Retrieving values from the parameter my $x = shift; my $y = shift; # Number till which the series is to be printed my $n = shift; # Check for the end value if ($y > $n) { return 1; } # Printing the number print " $y"; # Recursive Function Call fib($y, $x + $y, $n); } # Driver Code # Number till which series is to be printed $a = 5; # First two elements of the series $c = 0; $d = 1; print "$c"; # Function call with required parameters fib($c, $d, $a); Here is how the program works : Step 1- Function fib() is called with 3 parameters starting values which will be 0 and 1 while $n is a number till which a series is to be printed Step 2- These values are transferred in the form of an array whose values are retrieved with the use of shift. Step 3- At each call first two values are retrieved using shift and these values are stored in scalars x and y. Now these two values are added to get the next value in the series. This step continues till the value reaches the ending value provided by the user. Another approach using Recursion to print Fibonacci Series in Perl - Perl sub fibonacci { my($num) = @_; if ($num < 2) { # base case return $num; } return fibonacci($num-1) + fibonacci($num-2); } print("Enter the number till the series will be printed : "); $num = <>; for ($i = 0; $i < $num; $i++){ print(fibonacci($i)," "); } Output - Create Quiz Comment R rupanisweety Follow 0 Improve R rupanisweety Follow 0 Improve Article Tags : Perl Algorithms-Recursion Explore BasicsPerl Programming Language2 min readIntroduction to Perl7 min readPerl Installation and Environment Setup in Windows, Linux, and MacOS3 min readPerl | Basic Syntax of a Perl Program10 min readHello World Program in Perl3 min readFundamentalsPerl | Data Types3 min readPerl | Boolean Values3 min readPerl | Operators | Set - 112 min readPerl | Operators | Set - 27 min readPerl | Variables4 min readPerl | Modules3 min readPackages in Perl4 min readControl FlowPerl | Decision Making (if, if-else, Nestedâif, if-elsif ladder, unless, unless-else, unless-elsif)6 min readPerl | Loops (for, foreach, while, do...while, until, Nested loops)7 min readPerl | given-when Statement4 min readPerl | goto statement3 min readArrays & ListsPerl | Arrays6 min readPerl | Array Slices3 min readPerl | Arrays (push, pop, shift, unshift)3 min readPerl List and its Types4 min readHashPerl Hash4 min readPerl | Hash Operations8 min readPerl | Multidimensional Hashes6 min readScalarsPerl | Scalars2 min readPerl | Comparing Scalars6 min readPerl | scalar keyword2 min readStringsPerl | Quoted, Interpolated and Escaped Strings4 min readPerl | String Operators4 min readPerl | String functions (length, lc, uc, index, rindex)4 min readOOP ConceptsObject Oriented Programming (OOPs) in Perl7 min readPerl | Classes in OOP6 min readPerl | Objects in OOPs6 min readPerl | Methods in OOPs5 min readPerl | Constructors and Destructors4 min readPerl | Method Overriding in OOPs6 min readPerl | Inheritance in OOPs7 min readPerl | Polymorphism in OOPs4 min readPerl | Encapsulation in OOPs6 min readRegular ExpressionsPerl | Regular Expressions2 min readPerl | Operators in Regular Expression4 min readPerl | Regex Character Classes3 min readPerl | Quantifiers in Regular Expression4 min readFile HandlingPerl | File Handling Introduction7 min readPerl | Opening and Reading a File4 min readPerl | Writing to a File3 min readPerl | Useful File-handling functions2 min read Like