# 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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

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

Learn PCB Designing: PCB DESIGNING TUTORIAL