What is recursion in Scala function? How does it works?

0 votes
Can anyone help me to create recursion function in Scala?
Jul 25, 2019 in Apache Spark by Disha
45 views

1 answer to this question.

0 votes

Hey,

A Scala function involves recursion when it makes a call to itself. Let’s take an example:

scala> def factorial(n:Int):Int={

| if(n==1)

| {

| return 1

| }

| n*factorial(n-1)

| }

factorial: (n: Int)Int

scala> factorial(6)

res0: Int = 720

scala> factorial(1)

res1: Int = 1

scala> factorial(4)

res2: Int = 24

scala> factorial(10)

res4: Int = 3628800

This Scala function correctly calculates the factorial of an integer one or greater.

answered Jul 25, 2019 by Gitika
• 26,310 points

Related Questions In Apache Spark

0 votes
1 answer

What is function currying in Scala?

Hi, With Scala Currying, we can take a ...READ MORE

answered Jul 26, 2019 in Apache Spark by Gitika
• 26,310 points
75 views
0 votes
1 answer

What is tail-recursion in Scala?

Hey, Recursion is when a function makes a ...READ MORE

answered Jul 26, 2019 in Apache Spark by Gitika
• 26,310 points
64 views
0 votes
1 answer

How 'Scala Higher Order function' works in Scala?

Hey, Scala allows the definition of a higher-order function. These ...READ MORE

answered Jul 30, 2019 in Apache Spark by Gitika
• 26,310 points
57 views
0 votes
1 answer

Spark 2.3? What is new in it?

Here are the changes in new version ...READ MORE

answered May 28, 2018 in Apache Spark by kurt_cobain
• 9,310 points
105 views
+1 vote
1 answer
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,870 points
4,568 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,870 points
649 views
+1 vote
11 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
25,911 views
0 votes
1 answer

How does Closure function works in Scala?

Hi, A closure in Scala is a function ...READ MORE

answered Jul 26, 2019 in Apache Spark by Gitika
• 26,310 points
46 views
0 votes
1 answer

How to use yield keyword in scala and why it is used instead of println?

Hi, The yield keyword is used because the ...READ MORE

answered Jul 5, 2019 in Apache Spark by Gitika
• 26,310 points
113 views