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
357 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
• 65,870 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,870 points
349 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,870 points
301 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,870 points
305 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
331 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
8,943 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
1,604 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
80,531 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,870 points
223 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,870 points
1,022 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP