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

593 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’ve been programming since the eighties, so when I started we only had functional and procedural programming – OOP didn’t become much of a thing until the late eighties, and really took off in the nineties.

As others have said, functional programming is centered around functions – functions take arguments as input, carry out calculations/logic based on the arguments, and return an output. In really strict functional programming languages there are no persistent side effects – the function doesn’t alter any of the inputs, and doesn’t change the global state of the program. All it does is return a value or values up the chain to the calling function.

Functional programming is excellent for solving problems that translate well to mathematical concepts – matrix algebra (3d graphics, bitcoin mining), graph theory problems (vehicle navigation), statistical modeling, etc.

OOP came to be because there are a lot of problems that are more intuitive to think of in terms of objects with sets of behaviors. If you have a horse in your game and you want your horse to eat some grass, it’s more intuitive to say horse.eat(grass) rather than setting up a bunch of functions to calculate the various things that should change in your program when the horse eats. It’s also self-documenting, assuming you use reasonable naming conventions, and easier for a programmer who comes later to read.

In a modern program you’ll likely see a mix of functional and object-oriented programming. For example, Swift on Mac/iOS is OOP but you’re still likely calling some lower level C functions for some graphics calculations or other legacy stuff.

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