Function Definition
def methodName(paramName: paramType, …): returnType = {//function body}
Rules:
- single statement doesn’t need {}
- semicolon after statement is optional
- return type is optional as scala interpreter will look at the last line as return type (no need return keyword)
- return type and the “=” can be omitted when the type is Unit (ie. void in Java)
- recursive function must specify the return type.
- A function with no parameters can be declared without parentheses, in which case it must be called with no parentheses.
- Vararg parameters are declared by appending an asterisk to the argument
- You can specify default arguments
Example
//this function will return true if character ‘l’ is passed in
def lls(p: Char): Boolean = { p == ‘l’ }
//return void but >1 statement, so braces are needed
def box(s : String) {
val border = “-” * s.length + “–\n”
println(border + “|” + s + “|\n” + border)
}
//varag parameters
def sum(args: Int*) = {
var result = 0
for (arg <- args) result += arg
result
}
//call it:
val s = sum(1, 4, 9, 16, 25)
//1 to 5 is a range not Int as expected for single argument. Use _* to tell it is arg sequence
val s = sum(1 to 5: _*)
//bounded array input - T is subclass of Number, input is T varying array
def sum[T <: Number] (as: T*): Double = as.foldLeft(0d)(_+_.doubleValue)
//default arguments
def decorate(str: String, left: String = “[", right: String = "]“) = left + str + right
Function Call
Now you should have a good idea how to define a function in Scala. Lets look at some interesting areas related to anonymous function and compile syntax sugar.
Lets start from defining a function:
def lls(p: Char): Boolean = { p == ‘l’ }
In StringOps, there is a count function that takes the function type specified above:
def count (p: (Char) ⇒ Boolean): Int
StringOps is a scala way to extend the capability of Java String.
“Hello”.count(lls) // outputs 2
Functions with zero or one argument can be called without the dot and ( ). If it is single argument, you can use {} instead of ( ).
“Hello” count lls // outputs 2
Syntax Sugar
This kind of syntax sugar is quite confusing for an newbie like me. I believe it is created for a reason and I notice that it is used quite extensively in the area of DSL. Hopefully, there is syntax style standard develops on top of this and make life easier. Before seeing this, I need to try out all possible combinations and see how far Scala can take.

Here I demonstrate the rules are correct and > 1 space can be added as long as it doesn’t confuse the compiler.
Function Literal
(x:Int, y:Int) => //function body
Use of function literal – is like an anonymous function in java.
“Hello” count { p:Char => p == ‘l’ } // outputs 2
Function literal is shorthand of object of class FunctionN where N is number of input parameter)
new Function1[Char, Boolean] {
def apply(p: Char): Boolean = { p == ‘l’}
}
You can bind Function Literal to variables:
val add = (a:Int, b:Int) => a + b
add(1, 2) // Result is 3
Function literals are useful for passing as arguments to higher-order functions. They’re also useful for defining one-liners or helper functions nested within other functions.
“Hello” count { p => p == ‘l’ } // outputs 2 (type inference knowing p is character)
“Hello” count { _ == ‘l’ } // still outputs 2 (if one parameter passed in and the name has no significant, you can use _)