Function Calls

Now, we'll look at how function calls are implemented in Scala.

Syntax Goodies

Scala provides flexible syntax and it is worth dedicating a few minutes to this concept.

Named Parameters

The following is a function, f(a:Int, b:Int). We can call this function using the named parameter syntax: f(a = 5, b=10). If we swap the parameters but leave the correct names, the method will still be correct.

It is possible to combine positional and named function calls—the first few arguments can be positional.

For example:

def f(x:Int, y:Int) = x*2 + y
f(x=1,y=2) // 4
f(y=1,x=2) // 5

Default Parameters

When specifying a function, we can set default parameters. Then, later, when we call this function, we can omit parameters and the compiler will substitute ...

Get Professional Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.