FP Module 2 - 101 Scala


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).

Define the double function
Define the plusOneThenDouble function
Create your playground on
This playground was created on, our hands-on, knowledge-sharing platform for developers.
Go to