What is functional programming, and how is it different to OOP?

597 viewsEngineeringOther

I’m a mid level Swift developer and I’ve only ever worked with the Object Orientated Programming paradigm.

I’ve heard about functional programming and I tried Googling what it is, but I couldn’t understand the resources that explained it. Also, reading functional code is even more confusing.

So I was hoping that someone who understood it could break it down in incredibly basic and simple terms, and the benefits/drawbacks that it has compared to OOP.

In: Engineering

8 Answers

Anonymous 0 Comments

I’m not sure how well I can do an ELI5 but maybe more like an ELI16. If you’re familiar with the idea of a math function, a functional program is built up through functions. Now, I’m not talking about the term function as we use it in procedural programming. I mean pure math functions.

Also I would say it’s more pertinent to compare and contrast FP to imperative and procedural programming in general than OOP in particular.

A simple example would be something like:

multiply(2, subtract(10, add(2, 3)))

This creates a function chain where add(2,3) = 5 gets evaluated. Then subtract(10, 5) = 5. Then multiply(2, 5) = 10.

Now you might be asking, don’t all programs have functions? At least, the term function gets thrown around a lot in other programming languages right?

Well, the thing is, the functions in a programming language like C are *side effecting*. What that means is that you can access variables and memory outside of the function through things like global variables or passing pointers or manipulating variables from a visible scope. What this means is that if you invoke the same procedure with the same inputs, the behaviour of that function might change based on the external conditions/state of the program.

The term “function” is also a bit overloaded because “functions” in languages like C and JavaScript or C++ or Java tends to be a catch all term for subroutines, methods, procedures, etc. that may not have any outputs at all (when you return nothing at all or return void).

In functional programming, a function must behave like a pure math function. It must have inputs. It must have a single output. Period. No scope. No side effecting behaviours. You can only manipulate the inputs.

So with pure functions, you strictly work with inputs, and you must get an output, and then pass that output to another function. You can think of it like a factory. The whole program consists of inputs that go onto a conveyer belt, get transformed into something else, then move onto the next step (function) of the process, and it keeps going like that you get the final output. One important thing is also that the same inputs will always give the same output, which is different from side effecting functions.

This is different from imperative/procedural programming, because imperative/procedural programming works more like a list of steps that branch out depending on conditions or overall state of the program. The steps can take any inputs and give any outputs in any order as long as you have the memory to store it. The inputs and outputs can change depending on the state of the whole program. There aren’t things like if statements or loops in FP, and so therefore no hierarchical scope.

Now, that’s not strictly true as there are explicit ways to have side effects and scope, but those are advanced topics in functional programming. In FP, conditionals exist but they’re are treated more like piecewise functions in mathematics. Loops are handled by recursion. Scope is handled through a concept called closures. Side effecting behaviour is handled by a tricky concept called Monads. Again, it gets very confusing and that stuff goes so far beyond a ELI5 level I couldn’t even begin to explain it, each concept would require its own ELI5 individually.

You are viewing 1 out of 8 answers, click here to view all answers.