Why do we pass a function as an argument to another function? | SoloLearn: Learn to code for FREE!

+6

Why do we pass a function as an argument to another function?

Is there any instance or special reason for implementing a code of that nature

10/29/2020 10:05:53 PM

Ifesinachi

14 Answers

New Answer

+12

Expanding on the solid answers posted by Ipang and Maxwell Anderson, the functions passed in as parameters are referred to as predicates in many functions. Predicates are expressions that return True or False when applied to an item in a collection of values. This is a different pattern for implementing common looping logic using a functional approach. Consider the following imperative code with for loop: def is_even(x): return x % 2 == 0 nums = [1,2,3,4] evens = [] for num in nums: if is_even(num): evens.append(num) The actual condition for filtering is based on even numbers: num % 2 == 0 Note: This expression is in a function called is_even(). The functional version uses this function as a predicate instead of the if condition. evens = list(filtered(is_even, nums)) The functional approach is a declarative alternative to the imperative version. You can also learn more about this by reviewing a concept known as Higher Order Functions (HOF).

+11

Here's a link on HOF I just found which happens to use a similar example with checking for even. https://medium.com/@bonfirealgorithm/beyond-the-for-loop-higher-order-functions-and-list-comprehensions-in-python-695b58ab71d3 UPDATE: I swear... the examples in this article are so similar to the ones I wrote ad-hoc for this post that it literally looks like I ripped off the examples. But the example is so simple and so common, there's only so many ways these could be written. 😉

+9

Since you mentioned Python, one instance is the map() function. The first argument is passes as a function or an in-line lambda to perform an operation on an iterable that's passed as the second argument. The map function is found in similar forms in other languages.

+9

Depending on the need, a function object can be passed as argument to another function so it can be invoked to act as (amongst which) - Sorting algorithm helper which decides whether or not two elements should be swapped and how they should be positioned. - Modifies elements in an iterable. - Filtering algorithm helper which decides whether something fits a certain criteria. Basically the function is given some data to work on, and was expected to return something that helps the caller function makes a decision.

+7

Ifesinachi Good luck with your journey. Just realize that Python is a very different type of language from most other languages. Hopefully, that won't make it a challenge for you in learning other languages when that time comes.

+6

Honestly, guys I just started python 3 course 2 days ago. I am just hoping to go far in it. I do love the general syntax of the language because of its easy nature, although i came across some codes and article that were difficult for me to understand. But I am really grateful for your answers. Thank you Maxwell Anderson, Ipang and you sir David Carroll

+5

Maxwell Anderson thanks for that Good that you mentioned the map() function👍

+5

Ipang I really do appreciate you for your elaboration Thanks 👍

+4

My pleasure bro Ifesinachi 👌

+4

Thank you so much David Carroll.👏

+3

for example if you want from a function to returne some value and you want to use this value in another part of your code or another function you can just pass it like a parameter

+3

there are a course explain that in solo

+1

Ahmed Boussalh Thanks I do appreciate the fact you explained it as simple as possible 👍

+1

mohamed Riad please I don't understand