Skip to content

Latest commit

 

History

History
59 lines (45 loc) · 1.25 KB

nested-functions.md

File metadata and controls

59 lines (45 loc) · 1.25 KB
layout title partof num next-page previous-page redirect_from
tour
Nested Methods
scala-tour
11
multiple-parameter-lists
higher-order-functions
/tutorials/tour/nested-functions.html

In Scala it is possible to nest method definitions. The following object provides a factorial method for computing the factorial of a given number:

{% tabs Nested_functions_definition class=tabs-scala-version %}

{% tab 'Scala 2' for=Nested_functions_definition %}

def factorial(x: Int): Int = {
  def fact(x: Int, accumulator: Int): Int = {
    if (x <= 1) accumulator
    else fact(x - 1, x * accumulator)
  }  
  fact(x, 1)
}

println("Factorial of 2: " + factorial(2))
println("Factorial of 3: " + factorial(3))

{% endtab %}

{% tab 'Scala 3' for=Nested_functions_definition %}

def factorial(x: Int): Int =
  def fact(x: Int, accumulator: Int): Int =
    if x <= 1 then accumulator
    else fact(x - 1, x * accumulator)
  fact(x, 1)

println("Factorial of 2: " + factorial(2))
println("Factorial of 3: " + factorial(3))

{% endtab %}

{% endtabs %}

The output of this program is:

{% tabs Nested_functions_result %}

{% tab 'Scala 2 and 3' for=Nested_functions_result %}

Factorial of 2: 2
Factorial of 3: 6

{% endtab %}

{% endtabs %}