Click here to Skip to main content
13,256,260 members (40,632 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 12 Sep 2013

Higher Order Functions

, 13 Sep 2013
Rate this:
Please Sign up or sign in to vote.
Attempt to explain how higher-order functions are used in understanding functional programming concepts.


Most of my background is in imperative programming using C# and C++. Presently I am learning functional programming, and so created a simple program to demonstrate higher order functions. Functional programming uses declarative code, because this allows the developer to focus more on what he wants the program to do rather than how exactly the developer wants the solution to be constructed. Functional programs make extensive use of higher-order functions, which are functions that can take other functions as arguments or return them as results.

Using the code

Imagine you wanted to create a program that performs two different calculations, but the calculations differ only by the operation performed and the initial value. Consider these two calculations:

3 + (4 + (5 + (6 + (7 + (8 + 0)))))

And this,

3 * (4 * (5 * (6 * (7 * (8 * 1)))))

A traditional imperative software developer could possibly decide to create a base class called Calculation, and several derived classes specified by names such as Multiplication and Addition. The problem with this solution is that it is much too complicated for such a basic requirement. Instead, let`s use higher-order functions:

Here is the recursive function that is operation agnostic:

private static int PerformAggregation(Func<int,> operation, int initial, int start, int end)
    if (start > end) 
        return initial;
    int intermediate = PerformAggregation(operation, initial, start + 1, end);
    return operation(start, intermediate);

To perform the operations, the developer can use generic delegates:

Func<int,> aggregateMethod1 = delegate(int i, int j)
    return i + j;

Func<int,> aggregateMethod2 = delegate(int i, int j)
    return i * j;

Or lambda functions:

Func<int,> aggregateMethod1 = (i, j) => i + j;
Func<int,> aggregateMethod2 = (i, j) => i * j;

The developer could then make use of these functions like so:

Console.WriteLine("Addition: {0}", PerformAggregation(aggregateMethod1, 0, 3, 8));
Console.WriteLine("Multiplication: {0}", PerformAggregation(aggregateMethod2, 1, 3, 8));

Points of Interest

Object-oriented imperative programs are constantly changing state, so many extra precautions need to be taken so that no two processes or threads are in the same critical section at the same time. Functional programs use immutable objects to perform their operations, so this allows for parallelism without requiring all the extra work of creating semaphores to prevent race conditions.

As I have been learning functional programming, I've written some simple test programs like the one above. Hopefully you will find this article helpful!


  • 2013.08.30: Initial post. 
  • 2013.09.13: Added sample on use of functions. 



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Canada Canada
"O *righteous Father, [1]although *the world has not known You, yet I have known You; and these have known that *You sent Me; and *I have made Your name known to them, and will make it known, so that *the love with which You loved Me may be in them, and I in them." (Jesus) John 17:25-26 NASB

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
Empiric13-Sep-13 13:23
memberEmpiric13-Sep-13 13:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 13 Sep 2013
Article Copyright 2013 by john_1726
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid