# Functional programming

Reading Time: 7 minutes

Functional programming helps to solve problems in an easier way. Unlike other programming paradigms, such as imperative programming, functional programming focuses on what needs to be done rather than how it should be done.

Functional programming also relies on higher-order functions, which are functions that take other functions as arguments or return functions as results. This allows for the composition of functions, which can simplify code and make it more expressive.

Higher-order functions: They are a key concept in functional programming and can be used to create more modular and reusable code.

A higher-order function that takes a function as an argument is often called a “callback function”. The higher-order function will call the callback function at some point during its execution, typically to perform some operation or transformation on the data.

For Example:

```public class HigherOrderFunctionExample {

public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);

Function<Integer, Integer> square = x -> x * x;
Function<Integer, Integer> cube = x -> x * x * x;

System.out.println(apply(numbers, square)); // Output: [1, 4, 9, 16]
System.out.println(apply(numbers, cube)); // Output: [1, 8, 27, 64]
}

public static List<Integer> apply(List<Integer> list, Function<Integer, Integer> func) {
for (int i = 0; i < list.size(); i++) {
list.set(i, func.apply(list.get(i)));
}
return list;
}
}```

In this example, there are two higher-order functions square and cube that take an integer argument and return its square and cube, respectively. The apply function takes a list of integers and a function that takes an integer and returns an integer and applies the function to each element of the list. The apply function uses a Function interface from the Java Standard Library, which is a functional interface that takes one argument and returns a result. This allows us to use lambda expressions as arguments to the apply function.

Another key aspect of functional programming is the use of immutable data. In functional programming, data is treated as immutable, which means it cannot be changed once it is created. This eliminates the possibility of unexpected side effects caused by mutable data and can improve the reliability and predictability of code. Functional programming languages include Haskell, Scala, and Lisp, as well as features of other languages like Java and Python. Functional programming has gained popularity in recent years, as it can simplify complex code, improve code quality, and make it easier to reason about software systems.

### “First-Class Functions in Functional Programming”

At the heart of functional programming is the concept of functions as first-class citizens. This means that functions can be passed as arguments to other functions, returned as values from functions, and even stored in variables. This makes it easy to create reusable, modular code that can be easily composed to solve complex problems.

### “Advantages of Immutable Data Structures and Pure Functions in Functional Programming”

One of the benefits of functional programming is that it encourages immutable data structures and pure functions. Immutable data structures are those that cannot be modified once they are created. Pure functions are those that always return the same output for the same input, without modifying any external state. These properties make it easier to reason about the behavior of the code and can help prevent bugs and unexpected behavior.

### “Characteristics and advantages of Recursion in Functional Programming”

Functional programming also encourages the use of recursion rather than iteration. Recursion involves calling a function from within itself, rather than using a loop to iterate over a sequence of values. This can lead to more elegant, concise code that is easier to understand and reason about.

Functional programming languages, such as Haskell and Clojure, have gained popularity in recent years due to their ability to handle concurrency and parallelism more easily than imperative languages. This is because the lack of mutable states makes it easier to reason about shared resources and avoid race conditions.

Overall, functional programming offers many benefits for developers, including modularity, composability, immutability, and concurrency. By emphasizing the use of functions and immutable data structures, functional programming can lead to code that is more robust, maintainable, and scalable.

## Benefits of functional programming

• Modularity
• Composability
• Immutability
• Concurrency

## Common functional programming languages

• Lisp
• Clojure
• Scala

Functional programming basically uses a declarative style of programming, now we can discuss what actually declarative style of coding is and how it is different from an imperative one’s!!

## Imperitive Vs Declarative programming

### “Understanding Imperative Programming and its Characteristics”

Imperative programming is a programming paradigm that emphasizes describing the steps that a program must take to solve a problem. Programs written in an imperative style typically use statements that describe the actions the program must perform to achieve its objectives. Imperative programs are typically written using constructs such as loops and if statements.

### “Understanding Declarative Programming and its Characteristics”

In a declarative program, the programmer expresses the desired results, and the program figures out the steps necessary to achieve those results. Declarative programs are typically written using constructs such as functions, rules, and constraints.

One of the primary benefits of declarative programming is that it can make code more concise and easier to read. Because the program focuses on what the program should accomplish rather than the steps it should take, it can be easier to understand the program’s intent. Additionally, declarative programming can make it easier to reuse code and reason about complex systems.

## Quick example illustrating Functional Programming in Java

```public class FunctionalProgrammingExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

// Use a lambda expression to filter the list to even numbers
List<Integer> evenNumbers = numbers.stream()
.filter(k -> k % 2 == 0)
.collect(Collectors.toList());
// Print the results
System.out.println("Even numbers: " + evenNumbers);```

This example demonstrates several common functional programming concepts:

• Using lambda expressions to define functions inline.
• Using higher-order functions like `filter`.
• Using immutable data structures like lists.

## Comparison between Functional programming and Purely Functional programming

Functional programming and Purely functional programming are two related but distinct programming paradigms. While both emphasize the use of functions to create software, Purely functional programming places additional restrictions on the use of functions. In Functional programming, functions are used to perform computations, manipulate data, and create abstractions. However, functions can have side effects or modify states. In contrast, Purely functional programming requires functions to be referentially transparent, meaning that the same input will always produce the same output and there are no side effects. Purely functional programming emphasizes immutability, meaning that values cannot be changed once they are created. This makes programs are written in Purely functional programming easier to reason about and test, as they do not have hidden state changes. However, it can also make some tasks more challenging, such as input/output or working with mutable data structures.

### Example showing the difference between functional programming and purely functional programming:

```public class FunctionalVsPurelyFunctional {

// Functional programming approach
public static int sum(List<Integer> list) {
return list.stream()
.mapToInt(Integer::intValue)
.sum();
}

// Purely functional programming approach
public static int sum(int[] arr) {
return sum(arr, 0);
}

private static int sum(int[] arr, int i) {
return i == arr.length ? 0 : arr[i] + sum(arr, i + 1);
}

public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] arr = {1, 2, 3, 4, 5};

// Functional programming approach
int sum1 = sum(list);
System.out.println("Functional programming approach: " + sum1);

// Purely functional programming approach
int sum2 = sum(arr);
System.out.println("Purely functional programming approach: " + sum2);
}
}```

In this example, we have two methods named `sum()` which both compute the sum of a list of integers. However, the first method takes an `List<Integer>` argument and uses the `stream()` method to perform the computation in a functional programming style. The second method takes an `int[]` argument and uses recursion to compute the sum in a purely functional programming style.

The functional programming approach is more concise and easier to read, but it relies on the use of mutable objects like streams, which are not purely functional. The purely functional programming approach, on the other hand, avoids mutable objects and uses recursion instead, which is a purely functional technique. However, it is less concise and can be harder to read.

Overall, the main difference between functional programming and purely functional programming in Java is that functional programming uses immutable objects and higher-order functions to achieve a functional style, while purely functional programming avoids mutable objects and uses only purely functional techniques like recursion to achieve purely functional style.

## Advantages of Functional Programming for Developers

Overall, functional programming offers many benefits for developers, including modularity, composability, immutability, and concurrency. By emphasizing the use of functions and immutable data structures, functional programming can lead to code that is more robust, maintainable, and scalable.

1. Immutable Data: In functional programming, data is treated as immutable, which means that it cannot be changed once it is created. This makes the code more reliable, as it eliminates the possibility of unexpected side effects caused by mutable data.
2. Declarative Code: Functional programming relies on declarative code, which describes what the code should do rather than how it should do it. This makes the code more concise and easier to understand.
3. Higher-Order Functions: Functional programming uses higher-order functions, which are functions that can take other functions as arguments or return functions as results. This allows for more modular and reusable code.
4. Concurrency: Functional programming is well-suited for concurrent and parallel programming, as it relies on immutable data and pure functions, which makes it easier to reason about concurrent code and avoid race conditions.

## Disadvantages of Functional Programming

1. Steep Learning Curve: Functional programming can have a steeper learning curve than other programming paradigms. This is because it requires a different way of thinking about programming, which can take time to get used to.
2. Performance Issues: Functional programming languages can suffer from performance issues when compared to imperative programming languages like C or C++. This is because functional programming languages often rely on recursion and higher-order functions, which can create a lot of overhead.
3. Limited Library Support: Although functional programming has been gaining in popularity, the number of libraries and tools available for functional programming languages can be limited when compared to other programming paradigms. This can make it harder to find the right tools for the job.
4. Debugging Challenges: Debugging functional programs can be more challenging than debugging imperative programs. This is because functional programs often rely on recursion, which can make it harder to follow the flow of the program and identify the source of bugs.

This was an introductory blog giving brief knowledge of what actually functional programming is, In our upcoming blogs we will try to cover this concept in more depth and how functional programming works.

For a better understanding of functional programming, you may also take reference to the below link:

Keep Learning Keep Growing!!