### What is it?

So what is functional programming? Straight from wiki,

Definition: “functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements”

In constrast, imprerative programming focuses on procedure, and control flow of statements.

The functional programming paradigm is used extensively in many places. In javascript, the \$.each(list, someFunction) , is an example. For each item in the list, do something by calling someFunction. If you notice, the result of the function on an element doesn’t effect the result of another. The result of the function, only depends on what input it gets, and it will produce the same result every time its executed. No side effects! Because of this, concurrency is safe as well.

Functional programming abstracts away how to do things, and focuses on what to do. Declarative!. Call someFunction for each item in my list, don’t worry about how, just do it!

### Let’s go!

Since we are going to use functions a lot more, lets give them some respect, and learn how to represent them.

In C# you can represent a function as a Func,Action or Predicate

1. Func<T1,T2,…Tn> - a function that takes parameters T1 to Tn-1, and returns type Tn
2. Action<T1,T2,…Tn> - a function that takes parameters T1 to Tn, and returns void
3. Predicate<T1,T2,…Tn> - a function that takes parameters T1 to Tn, and returns a boolean

We can define functions in different ways as well

1. delegate - delegate(){ //do something }
2. lambda expression - () => { // do something }

If you are using the delegate way, you would have to specify the parameter types explicitly. However, with lambda expressions, you can do without them

Lets understand the basics with some examples.

• Func. Lets define a function that will double a number. It will take an integer as parameter, and will return an integer. We can either use the delegate to define what the function is, or use a lambda expression

Now we can call the above functions

• Action. We can also assign functions to other functions. In the below example, MyConsoleWriteLine is our own function, which points to Console.WriteLine. The Console.WriteLine is nothing but a function that takes a string parameter, and returns void (Action)
• Predicate. Lets define a function that will check if a number is greater than zeros

Since we can represent a function like any other object, we can also pass them around to other functions. So much power!

Lets take another example to understand this. Consider a list, with a necessity to get the count of elements from the list, based on some condition. Lets assume this condition to be the part of the code that may be different for different developers.

While writing code, it may not seem immediately apparent, but imagine 20 different developers, writing their own functions, because their conditions are different from one another.

Every developer would have something like below

Now imagine if we could take item > 0 as a function, that is a parameter of CountBasedOnYourCondition. Bingo!

This way we reuse almost all of our code.

C# LINQ already provides us with many methods to do exactly this.

Considering the above example, we can use the Count extension method.

Neat!