this post was submitted on 27 Mar 2025
33 points (100.0% liked)

Out of the loop

12730 readers
3 users here now

A community that helps people stay up to date with things going on.

founded 2 years ago
MODERATORS
33
submitted 2 months ago* (last edited 2 months ago) by [email protected] to c/[email protected]
 

I've seen a couple posts in the top in the last 6 hours feed, and it seems like people are really up in arms about this functional programming stuff. Not really sure what it even is.

It looks like it's people writing bad programming or something? Like a lot of extra stuff that is not necessary?

EDIT: sorry everyone, I'm not a programmer and I don't know to much other than a little java and python. I guess I should have posted this in Explain Like I'm Five.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 11 points 2 months ago* (last edited 2 months ago) (3 children)

Let me try getting this to ELI5 levels:

Most programming languages use a way of programming called "procedural programming" (or a variation of that, for example object-oriented programming). A series of instructions gets executed step by step to modify the program's overall state. Imagine it like a cooking recipe. You have control structures like if, while and for that influence the order in which instructions are executed. There are functions (or methods in object-oriented programming) that let you reuse some instructions over and over but that's not quite functional programming.

I'll use a syntax similar to Java, C++ or C# as an example:

void squareAllElements(int[] array)
{
    for(int i = 0; i < array.size(); i++)
    {
        array[i] = array[i] * array[i];
    }
}

Functional programming is a bit more like what you know from maths class. There are still functions that take a list of inputs and produce an output but there is no state to be modified. Every operation must take everything it needs (apart from maybe constants) as parameters and produces new values/objects as an output. You say you know Java, so imagine that none of your methods returns void and every variable can only be assigned once. There are no loops but for example operations that let you separate the first element of a list from the rest or create a new list by applying an operation to all elements of an input. To make that easier, functions can take other functions as parameters.

So let me show the same example in a functional style. This is no specific language, just an example to help you imagine how it works.

int square(int x) => x * x;
int[] squareElements(int[] array) => applyToAllElements(array, square);

Functional programming has its own advantages and disadvantages. It can avoid a lot of bugs because functions can't have unwanted side effects. They just take inputs, return outputs and leave the rest of the world (including their inputs) unmodified. On the other hand, that makes other use cases where you actually want to have some state that changes over time more difficult.

Hope that helps.

Edit: some cleanup and more precise explanations.

Edit: replaced "paradigm" with "way of programming" to make @[email protected] happy.

[–] [email protected] 16 points 2 months ago (1 children)

ELI5

paradigm

You must know some terrifying five-year-olds.

[–] [email protected] 10 points 2 months ago

At some point, using actual five year old words just makes communication harder rather than easier. OP mentioned that they're at least somewhat familiar with some programming languages so I assumed they'd figure it out from context. But fine, I'll change it.

[–] [email protected] 6 points 2 months ago* (last edited 2 months ago) (1 children)

And another example that shows off pattern matching:

Procedural:

int minimumElement(int[] array)
{
    if(array.size() == 0)
    {
        return 0;
    }

    int minimum = array[1];
    for(int i = 1; i < array.size(); i++)
    {
        if(array[i] < minimum)
        {
            minimum = array[i];
        }
    }
    return minimum;
}

Functional (still, no specific language, just an example of what it might look like):

int min(int a, int b) => a < b ? a : b;
int minimumElement(int[] array) =>
    array match {
        [firstElement, ...rest] => min(firstElement, getMinimumElement(rest));
        [singleElement] =>singleElement;
        [] => 0;
    }
[–] [email protected] 7 points 2 months ago

To a layman like me it looks like procedural looks more like logical coding and the functional version looks more like a math problem

[–] [email protected] 3 points 2 months ago

If you made it through that lengthy post and are still interested, let me add that many concepts from functional programming languages have made it into other languages over the years. Personally, I'm most familiar with C# which has functions that take other functions as parameters, lambda functions (functions without a name that you can just define directly where you use them as a parameter), simple list operations that take functions (filtering, sorting, transforming...) and to some extent pattern matching. From what I've heard, newer versions of Java have much of that as well and some of those features have even made it into C++ (from C++11 onwards) though in that case the syntax is a bit hard to read.