# Currying function with example in Python

**Python currying function**: Here, we are going to learn about the **currying function with example in Python**.

Submitted by Yash Khandelwal, on March 31, 2019

In terms of Mathematics and Computer science, **currying** is the approach/technique by which we can break multiple-argument function to single argument function.

**Mathematical illustration: h(x)=g(f(x))**

The composition of two functions is a chaining process in which the output becomes the input for the outer function.

def currying( g , f ): def h(x): return g(f(x)) return h

In the technical term **"Currying is the process of transforming a function that takes 'n' arguments into a series of 'n' function that only takes the one argument each."**

In problem-solving approach, **currying** is to be done to simplify the programming i.e. execution of the function which takes multiple arguments into the single - single argument functions.

**Example code 1:**

def f(a): def g(b, c, d, e): print(a, b, c, d, e) return g #as in f it return g this is currying f1 = f(1) f1(2,3,4,5)

**Output**

1 2 3 4 5

**Explanation of the above code:**

Now, f(1) returns the function g(b, c, d, e) which, for all b, c, d, and e, behaves exactly like f(1, b, c, d, e). Since g is a function, it should support **currying** as well.

**Example code 2:**

def f(a): def g(b): def h(c): def i(d): def j(e): print(a, b, c, d, e) return j #return to function i return i #return to function h return h #return to function g return g #return to function f f(1)(2)(3)(4)(5)

**Output**

1 2 3 4 5

**Explanation of the above code:**

In the code we have, X(a,b,c,d,e) = f(g(h(i(j(a,b,c,d,e))))

Here, the concept is of nesting of one function to another and hence the result of one function get stored or recorded in another function as a chain of functions.

**Note:** Now f(a,b,c,d,e) is no more i.e. now f no more take 5 arguments.

**Python example of currying function to convert INR to pounds**

# program to covert the Indian Rupee to Pound # Demonstrating Currying of composition of function def change(b, c): def a(x): #by currying function return b(c(x)) return a def IR2USD(ammount): return ammount*0.014 # as 1IR=0.014USD def USD2Pound(ammount): # Function converting USD to Pounds return ammount * 0.77 if __name__ == '__main__': Convert = change(IR2USD,USD2Pound ) e = Convert(565) print("Conveted Indian Rupee to Pound:") print('565 INDIAN RUPEES =',e,'PoundSterling')

**Output**

Conveted Indian Rupee to Pound: 565 INDIAN RUPEES = 6.0907 PoundSterling

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