How does declarative programming differ from procedural programming?
The fundamental difference between declarative and procedural programming lies in the focus of the code: whether it describes the desired result or the specific steps to achieve it.
Procedural Programming
Procedural languages function like recipes, providing step-by-step instructions for a computer to follow. They rely on three primary control structures to manage the flow of execution:1
* Sequence: Executing instructions one after another.1
* Conditional: Using logic (such as IF-THEN-ELSE) to follow alternative paths.1
* Iterative: Using loops to repeat steps until a condition is met.1
These languages focus on the "how," requiring the programmer to manage the specific bookkeeping of variables and execution flow.1
Declarative Programming
Declarative languages, also known as nonprocedural, focus on "what" is to be done rather than the specific implementation steps. There is typically less distance between the program's specification and its execution. Common types include:2
* Logic programming languages: Such as PROLOG, which define programs as sets of logical relations that an inference engine uses to answer queries.2
* Functional languages: Such as LISP, which construct programs by applying functions to arguments in a mathematical style.2