Over a million developers have joined DZone.

Scala: Introduction to Functions

DZone 's Guide to

Scala: Introduction to Functions

Here's an intro to Scala functions, with a look at function syntax, and in-depth examples.

· Java Zone ·
Free Resource

Alex_ZvolinskiyScala combines two programming paradigms: object oriented and functional. As a result you get the best from the two worlds. From the one side you can describe models as objects, and from the other side you can apply functions to them. And as you may guess, it’s really awesome! Let’s discover the basics of Scala functions.

Every time you need to perform some business logic or a simple action, start writing a function. With the help of functions you can do almost everything. Why only “almost”? Because to describe a model you still need use classes and objects.

Function Syntax

Functions can work with arguments and perform actions with them. Functions can return values. You can make a composition from multiple functions in order to complete one complex task, and all this is wrapped in a flexible syntax. So how do we create a function in Scala? Here is what a simple function declaration looks like:

def welcomeUser(name: String): Unit = {
    println("Hello, "+name)

This function prints a greeting. The greeting contains a name which is passed to the welcomeUser function as a parameter. And what about that weird word Unit in the first line? It indicates that the function doesn’t return anything.

After a function is declared we can use it. In order to call a function we need to write its name and pass parameters into it.

//the output is "Hello, Alex"

Here is another example of a Scala function:

def isEven(number: Int): Boolean = {
    if (number % 2 == 0)

The function above accepts an integer as a parameter and returns a boolean value. It’s logic is pretty trivial - it checks if the number argument is even or odd.

Here is a general pattern for a function declaration in Scala:


At first you need to write a def keyword. The next step is to write a function name. If the function needs some arguments you write them in the brackets in a format name: Type, otherwise brackets remain empty. Then you specify a return type of the function. And finally you write a function body.

More Function Examples

Now let’s consider several more examples of functions. We can start from the pretty useless function, which doesn’t has any arguments and prints some string:

def uselessPrint(): Unit = { println("Ho-ho-ho") }

What about a function which takes more than one parameter?

def taxCalculation(amount: Double, tax: Int): Double = {
    (amount / 100) * tax

In the example above we create the taxCalculation(amount: Double, tax: Int) function, and it accepts two parameters and calculates taxes based on them. Pay attention to the function body. Its last line has a Double type (after calculation). A type of the last line (last expression) in the method is a type which a function returns.
Here is what this function looks like in the REPL:


Scala allows us to omit a return type in a function declaration. But Scala knows what function returns due to type inference. We have discussed the type inference in the previous post about variables.

def stringCutter(str: String, length: Int) = {
    val result = str.substring(0, length)

Here is what happens in the REPL when we declare this function and use it:


There are a lot of syntax elements which can be omitted when you are working with functions in Scala, e.g. curly brackets if a function body suits well for a single line. Let’s rewrite the taxCalculation(amount: Double, tax: Int):

def taxCalculation(amount: Double, tax: Int) = (amount / 100) * tax

Functions Under a Microscope

As was said in the previous post, everything in Scala is an object. Functions are not exception. So actually, all functions which we have demonstrated in the current article are objects. Scala has some sort of abstractions for functions depends on number of arguments, starting from 0 (function without arguments) to 22 (yeah, imagine an ugly function with 22 parameters!).

Using these abstractions you can also define functions, but it’s not an elegant way - just look at this:

val taxCalculation = new Function2[Double, Int, Double] {
    def apply(amount: Double, tax: Int): Double = (amount/100)*tax

This is the absolute equivalent of the function which calculates taxes in the previous paragraph. This code sample is provided just for demonstration of the functions nature - they are objects. Here is how it looks like in the REPL:



Functions in Scala are the smallest building blocks for business logic. They are really powerful and can be used in many situations for different purposes. In this post we overviewed the basics of functions and their syntax. But there are a lot of things which you need to know about them, e.g. what is function composition or how to pass functions as an argument to another function.

scala ,functional programming

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}