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

0 votes
Can anyone help me to create recursion function in Scala?
Jul 25 in Apache Spark by Disha
24 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 by Gitika
• 25,340 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 in Apache Spark by Gitika
• 25,340 points
28 views
0 votes
1 answer

What is tail-recursion in Scala?

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

answered Jul 26 in Apache Spark by Gitika
• 25,340 points
31 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 in Apache Spark by Gitika
• 25,340 points
26 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,260 points
64 views
0 votes
1 answer
0 votes
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,710 points
3,321 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,710 points
395 views
0 votes
10 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
16,363 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 in Apache Spark by Gitika
• 25,340 points
28 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 in Apache Spark by Gitika
• 25,340 points
72 views