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
685 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.

If you want to know more about Apache Spark Scala, It's highly recommended to go for the Apache Spark Certification today.

answered Jul 25, 2019 by Gitika
• 65,910 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
• 65,910 points
613 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
• 65,910 points
560 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 31, 2019 in Apache Spark by Gitika
• 65,910 points
559 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,390 points
631 views
+1 vote
2 answers
+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
• 11,380 points
10,559 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
• 11,380 points
2,185 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
104,215 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
• 65,910 points
433 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 6, 2019 in Apache Spark by Gitika
• 65,910 points
1,679 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP