# Functional Programming

## Introduction

Functional programming is a declarative programming paradigm in that programming is done with expressions or declarations instead of statements. In functional programming code, the output value of a function depends only on its arguments, so calling a function multiple times with the same value for an argument always produces the same result.

One of the easiest way for the development of functional programming is making a program by eliminating changes in state that do not depend on function inputs which are called side effects.

## Key Concepts of Functional Programming

### First class function and high order function

In functional programming, the language supports passing functions as arguments to another functions, returning them as values from functions and assigning them to variables or storing them into data structure. This is what a first class function is meant to be. So first class functions are first-class variables.

With first-class functions, the names of functions do not have any special status; they are treated like ordinary variables with a function type.

A higher-order function is a function that does at least one of the following:

• takes one or more functions as arguments (i.e. procedural parameters)
• returns a function as its result

First-class functions are a necessity for the functional programming style, in which the use of higher-order functions is a standard practice.

A simple example of a higher-ordered function is the map function, which takes, as its arguments, a function and a list, and returns the list formed by applying the function to each member of the list. For a language to support map, it must support passing a function as an argument.

The distinction between the first-class function and higher-order function is subtle: “higher-order” describes a mathematical concept of function that operates on other functions, while “first-class” is a computer science term that describes programming language entities that have no restriction on their use and first-class functions can appear anywhere in the program.

Thus first-class functions can appear anywhere in the program that other first-class entities like numbers can, including as arguments to other functions and as their return values.

For example,

``````local twice = function(f,v)
return f(f(v))
end

return v + 3
end

### Pure Functions

Pure functions have no side effects and no state.

Calling the pure function again with the same arguments returns the same result. This can enable caching for memory optimizations.

The evaluation of any pure expression is thread-safe.

For example,

``````sum(x, y):
return x + y``````

Notice in the above `sum()` function, how the return value of the `sum()` function only depends on the input parameters `x` and `y`. Notice also that the `sum()` has no side effects, so it does not modify any state (variables) outside the function.

### Recursion

Iteration or looping in functional programming is usually accomplished via recursion. Recursive functions invoke themselves, letting an operation be repeated until it reaches the base case or terminating condition is met.

For example,

``````fibonacci(n):
if (n <= 1)
return 1
else
return fibonacci(n - 1) + fibonacci(n - 2)``````

### Referential Transperancy

Functional programs do not have assignment statements, i.e., the value of a variable in a functional program never changes once defined. This eliminates any chances of side effects. So, functional programs are referentially transparent.

Consider an assignment statement `x = x + 10`, this changes the value assigned to the variable `x`. Let us say that the initial value of `x` was `1`, then two consecutive evaluations of the variable `x` yields `11` and `12` respectively. Clearly, the expression is not referentially transparent.

### Immutable Variables

In functional programming paradigm we cannot modify a variable once it has been initialized. Therefore immutable variables are needed in functional programming. Immutable variables makes it easier to avoid side effects.