DEV Community

Cover image for Higher-order functions in Python and C#
Moataz Allam
Moataz Allam

Posted on Edited on

Higher-order functions in Python and C#

In this article I will explain what is higher order function and how to implement it in programming languages like c# and python.

What is Higher Order Function(HOF):

so HOF is a function that either takes a function as an argument or returns a function. This type of function has implementations in many programming languages like javascript, python, Go, etc.

Some examples:

let's build very simple function called doOperation() which take a function as a first parameter and two numbers and will return a function that takes two numbers as an arguments,
we have also a function called sum() that returns summation of two numbers.

Python:

def doOperation(operation, num1, num2):
    return operation(num1, num2)

def sum(number1, number2):
    return number1 + number2

doOperation(sum, 2, 3)

-----------
Output: 5
-----------
🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026 🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026

Csharp

public int DoSomthing(Func<int,int,int> sum, int num1,int num2) =>
    sum(num1, num2);


public int sum(int number1, int number2) =>
    number1 + number2;


DoSomthing(sum, 2, 3)

----------------
Output: 5
----------------
🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026 🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026

in cshap example we use a built in delegate Func<int,int,int> which take 2 integer numbers and return integer to assign a sum function to it.

So with this implementation we already apply higher order function.

Another example

we want to create filter() function to filter an array based on function we can pass it for this filter() function.

Python:

def filter(arr, action):
   result= []
   for num in arr:
      if(action(num)):
         result.append(num)
   return result

def isOddNumber(num):
   return num % 2 != 0


filter([1, 2, 3, 4, 5], isOddNumber)

----------------
Output: [1,3,5]
----------------

🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026 🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026

Csharp

 public static int[] Filter(int[] arr, Func<int, bool> func) =>
    arr.Where(func).OrderBy(n => n).ToArray();

public static bool isOddNumber(int number) =>
   number % 2 != 0;


Filter([1,2,3,4,5], isOddNumber)

------------------
Output: [1,3,5]
------------------

🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026 🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026

Hope it to be useful and thanks for reading!

Top comments (7)

🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026
 
abdelrhmanadelahmed profile image
abdelrhman-adel-ahmed •

Very informative article from an engineer that have promising future 👏

🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026
 
moataz_allam profile image
Moataz Allam •

hope it be useful, and thanks for your kind words

🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026
 
refaatelabddev profile image
Refaat Elabd •

Very useful concept hope to read more from you soon 😍🖤

🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026
 
moataz_allam profile image
Moataz Allam •

thanks , I will write more articles so soon Insha Allah

🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026
 
mgjimmy profile image
MGJimmy •

Very good and useful article thanks 👏👏

🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026
 
moataz_allam profile image
Moataz Allam •

Thanks, hope it was useful

🔥 Jadwal & Hasil: moataz allam/higher order function using python and c oe - Info Live Streaming 2026
 
moataz_allam profile image
Moataz Allam •

ok I will write about performance soon