# Currying function in Scala

A function that has multiple functions each with one argument chained as an argument of one function. In this Scala tutorial, we will discuss **currying function in Scala** and how they are implemented using an example?

Submitted by Shivang Yadav, on June 28, 2019

## Currying function in Scala

**A currying function is a transforming function with multiple arguments transformed into single arguments**. A **currying function** takes two arguments into a function that takes only a single argument.

There are two syntaxes to define the **currying functions in Scala**.

**Syntax:**

def functionName(arg1) = (arg2) => operation def functionName(arg1) (arg2) = operation

**Syntax explanation:**

In the first syntax, the function takes arg1 which equals arg2 and then the operation is performed.

The first single argument is the original function argument. This function returns another function that takes the second of the original function. This chaining continuous for all arguments of the function.

The last function in this chain does the actual word of the function call.

**Example:**

object MyClass { def add(x: Int) (y: Int) = x + y; def main(args: Array[String]) { println("sum of x + y = " + add(25)(10) ); println("sum of a + b = " + add(214)(4564) ); } }

**Output**

sum of x + y = 35 sum of a + b = 4778

**Code explanation:**

The above code defines and uses a **currying function** named add this function simply adds two numbers and return their addition. But is defined based on how a **currying function** is defined. The call also sends two numbers like two different functions in the function call.

**Currying** is a little bit tricky concept and you need to properly understand it to master over this. But this concept is useful while programming some big programs in Scala.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL