Find out our product. Here!

Function Returning Function - FP Series

Discover the concept of functions returning functions, a key aspect of first-class functions. Enhance code flexibility and reusability.

Up to this point, we've explored how we can define functions in much the same way we define other data types, like numbers or strings. We've also seen how functions can be passed as arguments, akin to other data types.

Now, we'll delve into the powerful concept of functions returning other functions, a key aspect of first-class functions. This feature greatly enhances the flexibility of first-class functions.


To grasp the idea of functions returning functions, think of functions as "boxes" in programming, where you input data, and the box produces an output. For instance, a function named "double" takes an input, say 5, and yields another number, here 10.

Similarly, a function called "uppercase" takes a string input like "hello" and produces the same string in uppercase, "HELLO." Python, treating functions like other data types, allows you to create a "box" that returns another "box," which functions just like a regular function.


You input data and get different data as output. This concept is what happens when functions return functions.

In essence, when a function returns another function, think of it as a function creator or factory. Now, let's explore the syntax for defining a function that returns another function. While it may appear daunting initially, understanding it will make it straightforward.

Consider the simplest example: a function that returns a function to print a message to the console. Let's call this function "createPrinter," taking no arguments. Within this function, we define another function, "printer," which also takes no arguments and simply prints "Hello Functional!" to the console.

Finally, we return the "printer" function.

def createPrinter():
    def printer():
        print('Hello Functional!')

    return printer

myPrinter = createPrinter()

To use this, we create a new variable, "myPrinter," and assign it the result of calling "createPrinter" with parentheses. "myPrinter" now acts as a function, identical in functionality to "printer."

We can invoke it with parentheses, like "myPrinter()," and running the code would print "Hello Functional!" to the console.

Now, let's explore a more practical example. Suppose we have three functions, "double," "triple," and "quadruple," frequently used in our program to manipulate numbers.

This results in some repetition in our code, indicating a need for refactoring to enhance code reuse. Enter first-class functions.

We envision a function named "createMultiplier" that accepts a number, let's call it "a," as an argument. Within this function's scope, we define another function called "multiplier," also taking a number as an argument.

def createMultiplier(a):
    def multiplier(x):
        return x * a

    return multiplier

double = createMultiplier(2)
triple = createMultiplier(3)
quadruple = createMultiplier(4)


The "multiplier" function simply returns the result of multiplying "x" by "a." Finally, "createMultiplier" returns the "multiplier" function.

With "createMultiplier" in place, we can now replace our existing "double," "triple," and "quadruple" functions. Instead of defining them individually, we can use "createMultiplier" to create these variations more efficiently.

Read Also :
Holla, we share any interesting view for perspective and education sharing❤️

إرسال تعليق

© elgharuty. All rights reserved. Developed by Jago Desain