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

587 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

Functional programming is a form of declarative programming, which is usually contrasted with imperative programming. In imperative programming, you give the computer a list of instructions, which it carries out in sequence (OOP is a special case of imperative programming). In declarative programming, you give the computer a series of definitions and then ask it for something – it will work out how to combine the definitions to achieve the required result. Those definitions can be combined to create more complicated definitions, but they generally can’t be changed – you can’t say “x=2” and then “x=3” a few lines later, because “x=2” was the definition of x, rather than an instruction to store a “2” somewhere.

Functional programming emphasizes the role of functions: most of a program will consist of function definitions. Functions can take other functions as arguments, and recursion (calling a function from inside itself) is allowed, but typically only a special type called tail-call recursion, which is easy for the compiler/interpreter to reason about and optimize. Loops generally don’t exist, so recursion is used frequently.

In practice, functional programming languages often allow for some imperative stuff, just as imperative programming languages often have some functional stuff (e.g. many of them allow you to pass functions as arguments to other functions).

The advantages and disadvantages are pretty debatable, like with many other programming styles. For most people, the deciding factor is how popular a programming language is within their specific application area.

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