Imperative and functional programming


Imperative programming

In imperative programming, we say exactly what we want to happen. Each step changes the state.


We can call a function: \(y=f(x)\)

Here \(x\) are the local variables to be used.

However the global state may also affect the outcome, so we have:


As a result, calling the same function twice with the same inputs can have different outputs.

Side effects. If a function modifies the state outside of its local variables it has side effects.

Functional programming

With functional programming, we write functions to be called. These functions should not depend on the state, outside of local variables.

Side effects

If we remove side effects from all functions then functional programming has no global variables which could affect the output.