Find out our product. Here!

Passing Function as Arguments - FP Series

Discover the power of Python's first-class functions: pass functions as arguments for flexible and dynamic data processing.

Now that we've explored several ways to treat functions just like other data types such as numbers or strings, let's delve into how we can pass functions as arguments to other functions.

Up until now, you've been accustomed to passing arguments into functions to specify the data you want to operate on. For instance, when you pass two numbers into an "add" function, you're telling it which numbers to add together.

But what if, instead of specifying the data within the function, you could pass in arguments that dictate what actions should be performed on that data? This is where first-class functions come into play.

  def add(x, y):
    return x + y

def subtract(x, y):
    return x - y
  

To illustrate this concept, let's examine two simple functions, "add" and "subtract," defined here. Each of these functions takes two arguments and performs either addition or subtraction on them.

Now, imagine if we had another function where, instead of passing two numbers, say 2 and 3, directly into the function, we pass in a function that specifies how those numbers should be combined.

Should they be added, subtracted, multiplied, averaged, and so on? Here's how it would work:

First, we define a function called "combineTwoAndThree" that takes one argument, which is a function. In the body of this function, we return the result of calling this function on the fixed numbers 2 and 3.

def combineTwoAndThree(func):
    return func(2, 3)  
    
print(combineTwoAndThree(add))
  

Now, we can take our "add" and "subtract" functions and pass them as arguments to our "combineTwoAndThree" function. For example, if we pass in the "add" function, we'll get the sum of 2 and 3.

If we pass in the "subtract" function, we'll get the difference, which is -1. You can similarly use any other function that combines two numbers and returns a result.

Additionally, we can create another function, similar to "combineTwoAndThree," that operates on different types of data. For instance, we can create a function called "combineNames" that takes a function as an argument and combines two strings (first and last names) using various methods.

def combineNames(func):
    return func('Shaun', 'Wassell')

def appendWithSpace(str1, str2):
    return f'{str1} {str2}'

def getGovernmentFormNotation(first, last):
    return f'{last.upper()}, {first.upper()}'

print(combineNames(appendWithSpace))
print(combineNames(etGovernmentFormNotation))
  

One method might simply append them with a space, while another method could reverse the order and format it in a government-style notation.

In summary, first-class functions in Python allow you to pass functions as arguments to other functions, enabling dynamic and flexible data processing based on the functions you provide as arguments.

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

Post a Comment

© elgharuty. All rights reserved. Developed by Jago Desain