Currying Function in Scala


Learning Scala involves learning lots of new concepts and syntax. In this blog post you will learn one of the important concept know as Currying function in scala.


Definition : Currying is the technique of transforming a function with multiple arguments into a function with just one argument.

Let us understand with an example program

def multiply(num1:Int,num2:Int):Int={

In the above example we have a function by name “multiply” which takes two arguments i.e num1 and num2

Since scala supports curried functions, we can turn into curried version by simply separating out the arguments as shown below.

Short hand Curried function

def multiply(num1:Int) (num2:Int):Int={

Long hand Curried function

The above curried function is short hand for the code shown below:

def multiply(x: Int): (Int => Int) = {
  (y: Int) => {
    x + y

Let me explain the above code, If you observe the above code the return type of a function is another function i.e after “:” what you see is a return type hence the function returns (num2:Int):Int

Let us invoke the function

scala> val functionOutput = multiply(2)


functionOutput: Int => Int = <function1>

Invoking on a return function

scala> val output = functionOutput(3)


output: Int = 5


  1. Currying is the technique of transforming a function with multiple arguments into a function with just one argument.
  2. The single argument is the value of the first argument from the original function.
  3. The function returns another single argument function. This in turn would take the second original argument and itself return another single argument function. This chaining continues over the number of arguments of the original. The last in the chain will have access to all of the arguments and so can do whatever it needs to do.

Download code

Reference documentation

About the course

The Apache Spark and Scala Training program is our in-depth program  designed to empower professionals to develop relevant competencies and accelerate their career progression in Big Data technologies through complete Hands-on training on Spark and Scala.

Learn Apache Spark by developing industry standard projects by following Agile methodologies  | Apache Spark Training with Project

Related Post
Spark RDD : groupByKey VS reduceByKey let's look at two different ways to compute word counts, one using reduceByKey and the other using groupByKey: val words = Array( "a","b","c","a...

Naveen P.N

12+ years of experience in IT with vast experience in executing complex projects using Java, Micro Services , Big Data and Cloud Platforms. I found NPN Training Pvt Ltd a India based startup to provide high quality training for IT professionals. I have trained more than 3000+ IT professionals and helped them to succeed in their career in different technologies. I am very passionate about Technology and Training. I have spent 12 years at Siemens, Yahoo, Amazon and Cisco, developing and managing technology.