O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Function Values with Multiple Parameters

In the previous example, the function values received one parameter. Function values can receive zero or more parameters. Let’s take a look at a few examples to learn how to define function values with different numbers of parameters.

In its simplest form, a function value may not even receive any parameter, but may return a result. Such a function value is like a factory—it constructs and returns an object. Let’s take a look at an example of defining and using a zero parameter function value:

 
def​ printValue(generator: () => ​Int​) = {
 
println(s​"Generated value is ${generator()}"​)
 
}
 
 
printValue(() => 42)

For the method printValue function, we’ve defined the parameter generator as a function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required