The code in this package example defines a method called factorial that takes an Int argument. The method prints "hello world" when executed. The code also defines a variable called factorial that stores the result of calling the factorial method on successive integers. The code in this example computes the factorial of 10- first, it prints "computing factorial of 10- first I need factorial of 9" and then it computes the factorial of 9 by multiplying 9 by 9 and storing the result in the variable result. Finally, the code prints the value of result.
package example
object factorial extends App {
//println("hello world")
def factorial(n: Int): Int = {
if (n <= 1) return 1
else
{
//println("computing factorial of " +n + "- first I need factorial of "+ (n-1))
val result = factorial(n-1) * n
//println("computed factorial of" +n)
result
}
}
println(factorial(10))
}