Functional Programming in Simple Terms: Explained

In the next article, we would look at the characteristics of Functional Programming. This article assumes the user has basic knowledge of writing simple C# programs and understands delegates.
Functional Programming (FP) in simple words is "Declarative Programming". That means FP does not deal with how to do (lines of code for achieving some functionality) but speaks about what to do. Microsoft introduced FP support with the release of .NET 3.5. LINQ and Lambda expression are one of the implementations of FP.
Let us say we have a customers object and we want to determine a customer based on the city "London", in normal programming. To accomplish that we need to loop through each object and check if the == "London". But the same thing can be accomplished using the Lambda Expression without writing more lines of code (in fact a one-line statement) as below:
  1. //Class Customer  
  2.  public class Customers  
  3.  {  
  4.         public int CustomerId { getset; }  
  5.         public string CustomerName { getset; }  
  6.         public string City { getset; }  
  7.  }  
  8.  //creating list of customers  
  9.   List<Customers> customers =new List<Customers>  
  10.   {  
  11.    new Customers(){CustomerId = 1,City = "Delhi",CustomerName = "Praveen"},  
  12.    new Customers(){CustomerId = 1,City = "London",CustomerName = "Raheem"},  
  13.    new Customers(){CustomerId = 1,City = "Malvern",CustomerName = "Pradeep"}  
  14.   };   
  15.  //Lambda expression to filter customers based on city  
  16.  var findCustomerAtLondon = customers.Where(c => c.City == "London");  
All of us know how to write a delegate. Simply a delegate is a strongly typed function pointer which refers to a method that has the same signature as the delegate.
A simple example of a delegate is as follows:
  1. //a delegate which takes two int parameters a and b  
  2. //return type is an integer  
  3. delegate int MyDelegate(int a, int b);  
  4. public class Program  
  5. {  
  6.     /// <summary>  
  7.     /// Method to add two numbers  
  8.     /// </summary>  
  9.     /// <param name="a"></param>  
  10.     /// <param name="b"></param>  
  11.     /// <returns>integer</returns>  
  12.     public static int Sum(int a, int b)  
  13.     {  
  14.         return a + b;  
  15.     }  
  16.     /// <summary>  
  17.     /// Main Method  
  18.     /// </summary>  
  19.     /// <param name="args"></param>  
  20.     public static void Main(string[] args)  
  21.     {  
  22.         //creating instance of a delegat MyDelegate  
  23.         //this instance refers to the method Sum  
  24.         //both the signatures match  
  25.         MyDelegate myDelegate = Sum;   
  26.         int @delegate = myDelegate(5, 6);   
  27.         Console.WriteLine(@delegate);  
  28.         Console.Read();  
  29.     } 
Figure 1:
Sum of two numbers
So you can use strongly type delegates for referring to the methods if the signatures match. C# has another way of doing this; by using generic functional types (Functional Programming). It is represented as Func<T, Result>.
We can replace the above delegate instance with the following code:
  1. Func<intintint> f = Sum;   
  2. int @delegate = f(5, 6); 
Syntax of Func in detail
A function type is represented as Func<T1, T2…TN, Output> where T1...TN can be of any type that is input to the function and Output is the return type of the function. Wherever we use delegates, those can be replaced with function types.
We can also write the above code as the following using a lambda expression:
  1. Func<intintint> f = (a, b) => a + b;  
  2. int @delegate = f(5, 6); 
So, you should be able to refer to a method and its type can be used as a variable and it can return a type too.
Other than Func, we have two more generic function types. They are Action and Predicate.
An Action simply is a function type that returns nothing i.e. void.
A Predicate is a function type that returns Boolean.
So you can represent an Action as:
Equivalent to Func<T1...TN, Void>. Do not try to create with this syntax as the compiler will give an error as void is not a return type.
Predicate can be represented as below
Equivalent to Func<T, bool>. You can try this and it works.
Action and Predicate in detail
Let us try to understand how we can use Action by modifying our previous code
Declare a class variable with Action type
Here the Action type takes an integer value and performs some functionality based on which function it is referring to.
private static readonly Action<int> write = Console.WriteLine;
We have defined a variable of type Action and it is pointing to the method WriteLine of the Console class. Since WriteLine does not return any value we can make Action to refer to the method.
Now you simply replace the code for Console.WriteLine with write variable.
  1. write(@delegate);  
  2. Console.Read(); 
Now let us see how we can use the Predicate.
  1. string name = "Ravi";  
  2. Action<bool> print = Console.WriteLine;  
  3. Predicate<string> myPredicate = (a) => (a.equals("Raju"));  
  4. bool predicate = myPredicate(name);  
  5. print(predicate); 
Here I have declared a string variable and then created another Action type which takes a Boolean value and prints it using Console.writeLine method. I have declared a Predicate Type and this refers to the lambda expression that checks if the value is equal to "Raju". This would return a false value.
Output of Predicate and Func using Action is as below
I am providing some more examples of Func, Action, and Predicate for a better understanding
We have the following functions available. Now try to identify what function type is to be used for each method.
  1. static string SayHello(string userName)  
  2. {  
  3.        return string.Format("{0} says Hello to you", userName);  
  4. }   
  5. static void PrintMyName(string myName)  
  6. {  
  7.       Console.WriteLine("My name is {0}",myName);  
  8. }  
  9. static bool CheckIfTwoStringsAreEqual(string a, string b)  
  10. {  
  11.       return (a.Equals(b));  
  12. }  
  13. static bool IsGreaterThan5(int a)  
  14. {  
  15.        return a > 5;  
The first method takes a string parameter and returns a string type. So, according to what we understood it must be referred by Func type.
  1. Func<stringstring> hello = SayHello;  
  2. string s = hello("Venu"); 
The second method takes a string parameter and returns void and so, it must be referred by Action Type.
  1. Action<string> print = PrintMyName;  
  2. print("Venu"); 
The third method takes two string parameters and returns a Boolean type. So, it can be referred by Func type.
  1. Func<stringstring,bool> check = CheckIfTwoStringsAreEqual;  
  2. bool b = check("abc""abc"); 
The fourth method takes only one int parameter and returns bool and so we can use Predicate for this.
  1. Predicate<int> predicate = IsGreaterThan5;  
  2. bool predicate1 = predicate(10); 
It can be replaced by Func type.
  1. Func<intbool> func = IsGreaterThan5;  
  2. bool func1 = func(5); 
Important points to note (Some extra information)
A Func type or Action can take only up to 4 parameters and Predicate can always take only one parameter. If you try to add extra parameters other than the supported compiler gives you an error message. According to best practices, any delegate should refer to a method that does not take more than 4 parameters. If any method is taking more than 4, you might have to refactor it.
The following are according to .NET 3.5.
If you are using .NET 4, then you will see the supported number of parameters as below
Predicate still takes one parameter only.
Action takes 16 parameters.
  1. public delegate void Action<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16);   
  3. public delegate bool Predicate<in T>(T obj); 
Func takes 16 parameters
  1. delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16); 
Also observe that if you click on "go to declaration", it shows that Action is a delegate. So, internally the generic function types are also delegates.


The main intention of the article is to get familiar with 3 generic function types: Func, Action, and Predicate. In the next article, we will try to understand the concept of Functional Programming in more detail.
Hope you liked this article.