Back
Close

FP Module 2 - 101 Scala

Bubu
3,359 views

Function (basics)

Function declaration

A Scala function declaration looks like :

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

This function is an abstract function (because there is no definition (~body) associated with the declaration).

Function definition

A Scala function definition is defined such as:

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

The return keyword could (and should) be omitted when the last statement of the function is returned. The return type clause can (almost the time) be omitted because Scala will often infer it (make an automatic deduction of the data type of an expression). You will see that in some occasion, it can't (you will see more about in the next section about function recursion).

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io