Evolution of programming paradigms (cont.)


Imperative Languages (e.g., C and Pascal)
Imperative programming uses statements that change a program’s state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates. The term is often used in contrast to declarative programming, which focuses on what the program should accomplish without specifying how the program should achieve the result.

#include <stdio.h>
int main( ) {    
  int number1, number2, sum;
  printf( "Enter two integers: " );
  scanf ( "%d %d", &number1, &number2 );
  sum = number1 + number2;   // calculating sum   
  printf( "%d + %d = %d", number1, number2, sum );
  return 0;
}


Declarative Languages (e.g., Prolog)
Declarative programming is a style of building the structure and elements of computer programs that expresses the logic of a computation without describing its control flow.

It attempts to minimize side effects by describing what the program must accomplish in terms of the problem domain, rather than describe how to accomplish it as a sequence of commands. Declarative programming often considers programs as theories of a formal logic, and computations as deductions in that logic space.
likes(mary,food).
likes(mary,wine).
likes(john,wine).
likes(john,mary).

 | ?- likes(mary,food). 
 yes.
 | ?- likes(john,wine). 
 yes.
 | ?- likes(john,food). 
 no.

Review: Programming Language Paradigms
    Which programming-language paradigm does Prolog belong to?

      Declarative
      Functional
      Imperative
      Object-oriented
        Result:




      Idle hands are the devil’s tools.