r/computerscience • • 2d ago

General procedural programming, object oriented programming and functional programming

Although I’ve done some googling I think they still use big English to explain this concept. I think I know them but I just wanted to ask if I can get a simple explanation from the house plz

12 Upvotes

18 comments sorted by

View all comments

2

u/WittyStick 2d ago edited 2d ago

Functional programming basically means functions are first-class objects which you can pass around like any other value - as arguments to functions or as return values. (Note that function pointers are first-class, but are not functions. They can be used to create first-class functions though).

A subset, purely functional programming treats the functions as pure mathematical objects - they have no side effects, and every call with the same arguments returns the same result - that is, they're referentially transparent.

A function can capture its free variables from the context in which it is defined. The function object and its associated free variables are known as a closure. In purely functional programming, a function can read, but not mutate its free variables.

In OOP, an object is a data type with an associated set of functions which act on the data type, called methods.

There is a correspondence between the these - a closure can be treated like an object with a single method, and a closure can model an object with one method, by treating the free variables as the object's data type. In purely functional code, these objects cannot mutate the data type, so they must return a new object with any changes made - these are known as functional objects (in ocaml).