NOTE:
This is a very old post that I’ve migrated from my blogger account.
I have matured since then and continue to learn, striving to remain humble with a mindset of humility.
Scala Function Basics
Defining a Basic Function
1
|
def functionName() = { /* function body */ }
|
Function with a Parameter
1
|
def functionName(param1: ParamType) = { /* function body */ }
|
Function with a Return Type
1
|
def functionName(param1: ParamType): ResultType = { /* function body */ }
|
Examples
Simple Function
1
|
def printHello() = { println("Hello, world!") }
|
Function with a Parameter
1
|
def echo(str: String) = { println(str) }
|
Function with a Return Type
1
2
3
4
|
def echo(str: String): Int = {
println(s"The length of '$str' is: ")
str.length
}
|
Reference
Fun with Scala Functions