Skip to content

DA1. Imperative Vs Non-Imperative Programming

Statement

  • Describe the differences between an imperative programming language and a non-imperative programming language. You must provide examples of each type of language as part of your response.
  • Further, discuss an example of where you would use an imperative language and a situation where a non-imperative language would be better. You discussion must explain why the selected language makes sense.

Solution

Imperative programming languages

  • The most popular programming languages are imperative languages. Includes languages such as C, C++, Java, Python, Fortran, Cobol, Scripting languages, etc.
  • It includes procedural programming languages, object-oriented programming languages, and scripting languages.
  • All imperative languages are based on Alan Turing’s Turing Machine.
  • The rely on the idea of assignment and state, where the load a value from a memory location into a register and then perform operations on that register, and then assign the new state of the register to the same (or another) memory location.
  • The assignment must be precise and explicitly stating the order of operations, nothing is left to the compiler to decide.
  • Ambiguity or uncertainty can cause issues, side effects, and bugs.
  • Procedural, and instructions are executed in order.
  • Variables are mutable, and can be changed at any time.
  • Easier to understand and debug.

Non-imperative programming languages

  • Non-imperative languages are also known as declarative languages. Includes languages such as Prolog (logic), Lisp, Haskell (functional), etc.
  • The programmer states the output they want, and let the compiler figure out how to get there.
  • They are not based on Alan Turing’s Turing Machine, but rather on Alonzo Church’s Lambda Calculus.
  • Includes functional programming languages, logic programming languages, and mathematical languages.
  • Difficult to understand and debug, but have less side effects.
  • All variables are immutable, and cannot be changed once assigned.
  • Referential transparency, where the same input will always produce the same output.
  • Lazy evaluation, where the compiler will only evaluate the expression when it is needed.

Imperative vs Non-imperative

  • The choice of language depends on the task at hand.
  • For simpler, smaller, and mathematical-like tasks, non-imperative languages are better (Ben-Ari, 2006).
  • As the non-imperative languages are more difficult to understand and debug and less performant, imperative languages are better for larger projects (UoPeople, 2023).
  • The level of abstraction is also an important factor as imperative languages allows for more abstraction than non-imperative languages.

References