Basic Functions, Parameters and Result Types


object basics {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  

  // Recursive Function
  def fact0(n:Int) : Int = {
  	val res = if(n<= 2) n else n*fact0(n-1)
  	res
  }                                               //> fact0: (n: Int)Int
  fact0(3)                                        //> res6: Int = 6
  
  // Recursive Function without block { }
  def fact1(n:Int) : Int =
  	if(n <= 2) n else n*fact1(n-1)            //> fact1: (n: Int)Int
  fact1(20)                                       //> res7: Int = -2102132736
  
  // Recursive Function using Long Type Parameter
  def fact2(n:Int) : Long =
  	if(n <= 2) n else n*fact2(n-1)            //> fact2: (n: Int)Long
  fact2(20)                                       //> res8: Long = 2432902008176640000
  
  // Recursive Function using BigInt Type Parameter
  def fact3(n:Int) : BigInt =
  	if(n <= 2) n else n*fact3(n-1)            //> fact3: (n: Int)BigInt
  fact3(100)                                      //> res9: BigInt = 9332621544394415268169923885626670049071596826438162146859296
                                                  //| 3895217599993229915608941463976156518286253697920827223758251185210916864000
                                                  //| 000000000000000000000
  // Function using String Type Parameter
  def fn3(x : String) : String = "Welcome " + x   //> fn3: (x: String)String
  fn3("All to the Big Data Workshop")             //> res10: String = Welcome All to the Big Data Workshop
  	
}