Home » Scala language

Recursion function in Scala

Recursion function is very important in functional programming. In this tutorial, we will study some basic of recursion function in Scala and see different implementations of recursion function with examples.
Submitted by Shivang Yadav, on June 24, 2019

Recursion function in Scala

Recursion function is a function that calls itself again and again until a specific condition becomes true. In functional programming, recursion plays a big role and being a functional programming language Scala also supports recursion function.

So, Recursion function is calling the same block of code again till the end condition becomes true. Most programs that require adding or doing some specific calculations multiple times use recursion. It is a better choice instead of an infinite while loop that need a break condition. In Scala break is prone to exceptions and programming is a little bit tricky so it is good to practice to use recursion function in place of it.

Syntax:

    def recFun(arguments){
	    if(condition )
		    return value
	    else
		    return argument * recFun(next_arguments)
    }	

Syntax explanation:

Here the recFun is a recursion function that uses a set of arguments. These set of arguments are mostly a single argument that is an integer that is to be used for calculation. Then in the body of the function, there is an if condition that is used to check whether to recall the recursion function or not. For TRUE value of the condition the final return is executed and the code flow goes back to the calling block of code. For FALSE value the condition calls the recursion function again doing the task specified (here we have used * ) by the program.

Examples:

1) Print Factorial of a number using Recursion in Scala

object myClass {
    def factorial(n: BigInt): BigInt = {  
      if (n == 1)
         1  
      else    
            n * factorial(n - 1)
   }
   def main(args: Array[String]) {
         println( "Factorial of " + 25 + ": = " + factorial(25) )
   }
}

Output

Factorial of 25: = 15511210043330985984000000

Example explanation:

The above code is to print the factorial of a number. Since factorial may take huge values, we have used BigInt for the result. To avoid overflow condition. This increase the memory of the code but is important as the factorial of 25 as an example will result in an error if Int would be used as a datatype.

2) Print Fibonacci of a number using Recursion in Scala

object myClass {
    def fibonacci(previous1 : Int, previous2 : Int , counter : Int , condition  : Int ){  
   	  print( ", "+(previous1 + previous2))
      if((counter+1) < condition){
          fibonacci(previous2, (previous1 + previous2), (counter+1) , condition)
      }
   }
   def main(args: Array[String]) {
         println( "Fibonacci series till " + 15 + ": = ")
         print("0 , 1")
         fibonacci(0,1,2,15)
   }
}

Output

Fibonacci series till 15: = 
0 , 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.