"On two occasions I have been asked, 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?'...I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question." 

- Charles Babbage (1864): Inventor of the Analytical Engine (the originator of digital computing)

**Functional Programming **defines another programming paradigm of the ‘declarative’ (as opposed to imperative) type of programming approaches defined within Computer Science.

This article explores functional programming for beginners, using the highly popular JavaScript ES6 language for reference.

In short, functional approaches to programming are defined as applying and composing of functions, in contrast to the imperative ‘line by line’ approach to programming used by historical Computer Scientists, such as Babbage.

This begs us to ask, what are functions?

Dictionary.com unpacks a function, in Mathematical definition as…

Also called correspondence, map, mapping, transformation. a relation between two sets in which one element of the second set is assigned to each element of the first set, as the expression y = x^2; operator.

https://www.dictionary.com/browse/function

If you haven’t studied advanced Mathematics, perhaps, beyond required schooling, or Computer Science formally (like myself); to decompose the meaning of functions in programming; it is simple to understand that we are taking a ‘set’ of defined values or ‘objects’ of data and mapping or transforming that data to another value.

We call the overarching ‘module’ that is created by transforming, for example, ‘x to y’ — a function.

Let me explain a little further herein. I will be using JavaScript ES6 functions, however I will stick to one form of expressing functions using function declarations (rather than arrow and anonymous functions), for simplicity. If you are interested in learning how to write functions in all forms within JavaScript, I suggest you take a thorough course on the subject.

What is a function in day to day use?

It is imagined that you have grasped some of the topics and syntax of computer programming at this stage, but perhaps are not sure about what a function is and how to use a function driven approach to get your programming needs done.

If you’ve encountered Microsoft Excel in the past, with a little training, you may have been taught how to use the auto ‘sum’ feature within this program.

This feature allows the user to highlight a group of cells containing numerical values and to simply click a button to automatically compute the ‘sum’ or total of all those numbers.

Image for post

Excel’s SUM Feature

In Excel, this essentially serves a purpose as a pre-written function. When we write =SUM(X) and hit return in a cell in Excel, with X being the value(s) we want to total; the mapping from X to Y is undertaken under the hood.

So perhaps, in Excel, what the function composition or expressions of SUM could be determined as, might look a little like this. Using the above screenshot of Excel, the values are denoted within brackets…

SUM = (10 + 20 + 30 + 15)
// Returns 75

The above is what you could define as an imperative approach to programming (the opposite of a functional approach). It is the function unpacked, written laboriously, but explicitly defined. In an imperative approach, we don’t refer to functions or control flow — we simply execute instructions top to bottom.

What if we had to undertake this process without using the SUM feature within Excel for hundreds or thousands of cells containing values?

You would have a very clearly defined program, written line by line, computing everything you explicitly defined, but perhaps it would take a much longer period of time than simply defining what the function SUM does and then using it once, or as many times as required.

This is, at it’s heart, what functional programming is all about…

To conclude the semantics of definition, a function is basically a transformation of values from X to Y. As we saw in SUM, a set of number or numbers (X) to a single total (Y). To add to this, we typically ‘return’ a value (Y) as a result of the function — so that the function serves a purpose in the program.

In programming parlance, functions are typically declared and ‘named’ — as is ‘SUM’ the name of a computation to total a set of values within Excel — in various computer programming languages, you can define a function to do whatever computation (or transformation) you wish and give it an apt name.

We aren’t limited to just summing numbers — we can write functions to merge arrays, unpack objects, iterate and filter values into new data structures and so on and so forth, the limit is really what the language is capable of doing with the given data structures.

#es6 #javascript #functional-programming #developer

What Is Functional Programming?
2.40 GEEK