Why do we pass a function as an argument to another function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

29th Oct 2020, 10:05 PM
Ebonam Ifesinachi
Ebonam Ifesinachi - avatar
13 Answers
+ 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).
30th Oct 2020, 1:18 AM
David Carroll
David Carroll - avatar
+ 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-JUMP_LINK__&&__python__&&__JUMP_LINK-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. 😉
30th Oct 2020, 1:22 AM
David Carroll
David Carroll - avatar
+ 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.
29th Oct 2020, 10:09 PM
Maxwell Anderson
Maxwell Anderson - avatar
+ 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.
29th Oct 2020, 10:21 PM
Ipang
+ 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.
30th Oct 2020, 1:44 PM
David Carroll
David Carroll - avatar
+ 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
30th Oct 2020, 9:49 AM
Ebonam Ifesinachi
Ebonam Ifesinachi - avatar
+ 5
Maxwell Anderson thanks for that Good that you mentioned the map() function👍
29th Oct 2020, 10:20 PM
Ebonam Ifesinachi
Ebonam Ifesinachi - avatar
+ 5
Ipang I really do appreciate you for your elaboration Thanks 👍
29th Oct 2020, 11:37 PM
Ebonam Ifesinachi
Ebonam Ifesinachi - avatar
+ 4
My pleasure bro Ifesinachi 👌
30th Oct 2020, 6:01 AM
Ipang
+ 4
Thank you so much David Carroll.👏
30th Oct 2020, 9:40 AM
Ebonam Ifesinachi
Ebonam Ifesinachi - avatar
+ 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
30th Oct 2020, 7:40 PM
Ahmed Bousaleh
Ahmed Bousaleh - avatar
+ 1
Ahmed Boussalh Thanks I do appreciate the fact you explained it as simple as possible 👍
31st Oct 2020, 6:07 PM
Ebonam Ifesinachi
Ebonam Ifesinachi - avatar
+ 1
mohamed Riad please I don't understand
31st Oct 2020, 6:08 PM
Ebonam Ifesinachi
Ebonam Ifesinachi - avatar