Function Declaration

The form of a function looks like:

def functionName ([list of parameters]) : [return type]

I have seen various types of declarations and initially it was difficult to sort our why certain functions are defined a particular way a basic understanding of the various ways to do this goes a long way.

General Syntax

def functionName ([list of parameters]) : [return type] = {
   function body
   return [expr]
}

A typical function in scala would be like

object sub {
   def subInt( a:Int, b:Int ) : Int = {
      var result:Int = 0
      result = a - b
      return result
   }
}

A function that does not have a return type should return a Unit. This is essentially a void in java. If you encounter this in code it tells you that nothing is going to be returned by this function.

object Obj {
  def printHello() : Unit {
    println("Hello, World!")
  }
}