At EasyTechJunkie, we're committed to delivering accurate, trustworthy information. Our expert-authored content is rigorously fact-checked and sourced from credible authorities. Discover how we uphold the highest standards in providing you with reliable knowledge.
Declarative programming is a way of specifying what a program should do, rather than specifying how to do it. Most computer languages are based on the steps needed to solve a problem, but some languages only indicate the essential characteristics of the problem and leave it to the computer to determine the best way to solve the problem. The former languages are said to support imperative programming whereas the latter support declarative programming.
One can consider the following example from SQL to get all the sales regions where profit exceeds a certain number, say, $700 US Dollars (USD), from the sales data.
Select Region, Profit from Sales where Profit > 700
This statement does not indicate what the SQL system has to do in order to get the data. The SQL system can go through each sales record and determine whether the condition is satisfied, or, it can quickly obtain the top few records from presorted data. The statement only indicates the nature of the desired data.
Prolog is a declarative programming language which indicates the logical relations between entities.
ancestor(M, C) :- mother(M, C)
ancestor(X, Z) :- mother(X, Y), ancestor(Y, Z)
The above program indicates truisms. If M is the mother of C then M is an ancestor of C. If X is the mother of some person Y who is an ancestor of Z, then X is an ancestor of Z. Given this and some mother-child facts, the program can answer questions about the whole maternal family tree.
During program execution, the Prolog system builds up a number of true statements, thus creating a knowledgebase. This knowledgebase is searched efficiently on its own. The creator of the above program does not specify how to search.
HTML and CSS are declarative programming languages. For instance, the HTML example <table border="1">, indicates a thin border. A CSS example is color: blue. This specifies the text color. As can be seen in these examples, HTML and CSS specify what should appear on a web page but not how to do so.
The advantage of declarative programming languages is mainly two-fold. The programs are concise; this makes it easy even for non-programmers to obtain solutions. In the SQL example above, an analyst or business support person can get the desired information. Similarly, laypersons can write acceptable web pages with simple HTML and CSS commands.
The second advantage of the declarative programming model is that repetitive imperative code that indicates how to solve things is provided in the computer system behind the scenes. Such code can be made highly efficient and can incorporate the best ideas from computing. It can take advantage of parallelism.