As a Gd re-entry task, I was assigned to give presentation on Programming Paradigms by giving examples:
Here is its slides:
Here are some programmings which will help you to understand these programming paradigms:
I consider here a single program i.e ‘Fibonacci Series’ to in some programming languages of different paradigms that will help you understand the difference between these paradigms:
Basic Programming Paradigms are:
1) Imperative Paradigm
2) Declarative Paradigm
Here is a brief description with programs:
1) Imperative Paradigm: Here we describe the sequential steps to solve the problem. i.e First Do this then Do that. i.e How we achieve the problem.
Example: I want a cup of tea, I will call my sister and tell her procedure about making a tea, i.e first take a tea pan, then put it on stove, then fire on the stove, add water, add suger, boil it, add milk…..so on….
So this style(paradigm) to achieve a problem(Tea in this case) is Imperative.
A fibonacci series program in Pascal language showing Procedural Paradigm.
PROGRAM Fibonacci; VAR First, Second, Temp : int64; count : integer; BEGIN First := 0; Second := 1; writeln(First); writeln(Second); for count := 0 to 10 do begin Temp := FIrst + Second; First := Second; Second := Temp; writeln(Temp); end END.
A fibonacci series program in fortran language showing Structural Paradigm. program Fibonacci integer :: f1,f2,f3,i i = 1 f1 = 0 f2 = 1 do f3 = f2 + f1 f1 = f2 f2 = f3 i = i + 1 if (f1<10) then print '(I1, A, $)', f1, ', ' elseif (f1<100) then print '(I2, A, $)', f1, ', ' else print '(I3, A, $)', f1, ', ' end if if (i==17) then exit end if end do print *, '...' end program Fibonacci
2) Declarative Paradigm: Here we describe ‘what to achieve, rather than How to achive’.
Example:I want a cup of tea, I will call my sister, and order her how to make a cup of tea. I am not descibing here producer to make a tea.
A fibonacci series program in Prolog language showing Logical Paradigm. fib(0, 0). fib(1, 1). fib(N, NF) :- A is N - 1, B is N - 2, fib(A, AF), fib(B, BF), NF is AF + BF.
A fibonacci series program in Hasksell language showing Functional Paradigm. fib :: Integer -> Integer fib n | n == 0 = 0 | n == 1 = 1 | n > 1 = fib (n-1) + fib (n-2) fibs :: [Integer] fibs = 0 : 1 : zipWith (+) fibs (tail fibs) fastfib n = fibs !! n