# Functional Programming vs Imperative Programming

Now that we have taken a look at functions in Haskell, let’s explore the concept of functional programming. In functional programming, the basic method of computation is the application of functions to arguments. Therefore, functional programming is best described as a programming style, and functional programming languages support and encourage this style.

On the other hand, in imperative programming the basic method of computation is changing stored values, i.e. functions in imperative programming languages are not purely mathematical functions, but rather a sequence of instructions that the program should follow to get to the result. To better understand this, let’s see how a task of calculating the sum of natural numbers between `1` and `n` would normally be handled by an imperative language, C:

```c
int sum = 0;
for (i = 1; i <= n; i++) {
    sum = sum + i;
}
```

The above program first initialises a variable `sum` to zero, and then loops (repeats the same action) through all the numbers from `1` to `n`, updating the stored value of  the `sum` variable on each iteration. In the case of `n = 3`:

```c
sum = 0;
{ first iteration }
i = 1;
sum = 1;
{ second iteration }
i = 2;
sum = 3;
{ third iteration }
i = 3;
sum = 6;
```

Now let's take a look at how that task could be handled by Haskell – we can achieve this with a combination of two functions:

1. `[n .. m]` – which produces a list of numbers from `n` to `m`, e.g. `[1..3] => [1, 2, 3]`
2. `sum`  – which produces the sum of a list

{% hint style="info" %}
`sum` is a predefined function in the Haskell standard library called Prelude, which is imported by default into all Haskell modules. Libraries are collections of functions already written by people to solve various problems, and we can use them without having to rewrite existing solutions ourselves.
{% endhint %}

```haskell
sum [1..3]
= { applying [..] }
sum [1, 2, 3]
= { applying sum }
1 + 2 + 3
= { applying + }
6
```

The above example shows that executing Haskell programs triggers a sequence of function applications – the basic method of computation in functional programming. In functional programming, the code tells the program what to calculate, but not explicitly how to get to the end result following a sequence of steps.

That brings us to another important thing – Haskell has no variable assignments. The equality sign `=`  in Haskell is not the assignment operator as in imperative languages, but instead the equivalent of the mathematical equal sign.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://haskell.hpmeducation.com/introduction/functional-programming-vs-imperative-programming.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
