You are on page 1of 15

PROGRAMMING PARADIGMS

A programming paradigm is a fundamental style of programming. A paradigm is a collection of abstract features that categorize a group of languages which are accepted and used by a group of programmers Paradigms differ in the concepts and abstractions used to represent the elements of a program (such as objects, functions, variables, constraints, etc.) and the steps that compose a computation (assignment, evaluation, continuations, data flows, etc.). A programming language is a systematic notation by which we describe computational processes to others. A language has three major components: y y y Language paradigm- set of general principles used by programmer to communicate a task to computer Syntax-collection of statements formed by following a set of rules. Semantics-means meaning of a program in that language

Programming paradigms of two types: 1. Imperative paradigm 2. Declarative paradigm IMPERATIVE PARADIGM Imperative programming is characterized by programming with a state and commands which modify the state. Imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way that imperative mood in natural languages expresses commands to take action , imperative programs define sequences of commands for the computer to perform. Imperative paradigms are categorized as: y y Procedural paradigms Object oriented paradigms

PROCEDURAL PARADIGMS Procedural programming is imperative programming in which the program is built from one or more procedures (also known as subroutines or functions). The terms are often used as

synonyms, but the use of procedures has a dramatic effect on how imperative programs appear and how they are constructed. A programmer can often tell, simply by looking at the names, arguments and return types of procedures (and related comments), what a particular procedure is supposed to do - without necessarily looking at the detail of how the procedure achieves its result.

ALGORITHMIC LANGUAGES
C Language

C (pronounced like the letter C) is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories . C is an imperative (procedural) systems implementation language. It was designed to be compiled using a compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. C was useful for many applications that had formerly been coded in assembly language. Characteristics
y y y y y There are a small, fixed number of keywords, including a full set of flow of control primitives: for, if, while, switch, and do..while. There are a large number of arithmetical and logical operators, such as +, +=, ++, &, ~, etc. More than one assignment may be performed in a statement. Function return values can be ignored when not needed. Declaration syntax mimics usage context. C has no "define" keyword; instead, a statement beginning with the name of a type is taken as a declaration. There is no "function" keyword; instead, a function is indicated by the parentheses of an argument list. User-defined (typedef) and compound types are possible. Heterogeneous aggregate data types (struct) allow related data elements to be accessed Enumerated types are possible with the enum keyword. Strings are not a separate data type, but are conventionally implemented as nullterminated arrays of characters. Low-level access to computer memory is possible by converting machine addresses to typed pointers. Procedures (subroutines not returning values) are a special case of function, returning the dummy type void. Functions may not be defined within the lexical scope of other functions A preprocessor performs macro definition, source code file inclusion, and conditional compilation. There is a basic form of modularity: files can be compiled separately and linked together, with control over which functions and data objects are visible to other files via static and extern attributes.

y y y y y y y y y

Complex functionality such as I/O, string manipulation, and mathematical functions are consistently delegated to library routines.

COBOL COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language. COBOL is designed for developing business, typically file-oriented, applications, and is not designed for writing systems programs. Distinct Features of COBOL The language is simple No pointers No user defined types No user defined functions Structure like data types File records are also described with great detail, as are lines to be output to a printer COBOL is self documenting Structure of COBOL COBOL programs are hierarchical in structure. Each element of the hierarchy consists of one or more subordinate elements. The levels of hierarchy are Divisions, Sections, Paragraphs, Sentences and Statements. There are 4 main divisions and each division provides an essential part of the information required by the complier. At the top of the COBOL hierarchy are the four divisions. The sequence in which they are specified is fixed, and must follow the order: y y y y IDENTIFICATION DIVISION supplies information about the program to the programmer and the compiler. ENVIRONMENT DIVISION is used to describe the environment in which the program will run. DATA DIVISION provides descriptions of the data-items processed by the program. PROCEDURE DIVISION contains the code used to manipulate the data described in the DATA DIVISION. It is here that the programmer describes his algorithm.

OBJECT ORIENTED PROGRAMMING PARADIGMS

A type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. Programmers can create relationships between one object and another. Features y Data Abstraction (specifies behavior)

y y y y

Encapsulation (controls visibility of names) Polymorphism (accommodates various implementations) Inheritance (facilitates code reuse) Modularity (relates to unit of compilation)

Features y Dynamic dispatch when a method is invoked on an object, the object itself determines what code gets executed by looking up the method at run time in a table associated with the object. This feature distinguishes an object from an abstract data type (or module), which has a fixed (static) implementation of the operations for all instances. It is a programming methodology that gives modular component development while at the same time being very efficient. Encapsulation (or multi-methods, in which case the state is kept separate) Subtype polymorphism Object inheritance (or delegation) Open recursion a special variable (syntactically it may be a keyword), usually called this or self, that allows a method body to invoke another method body of the same object. This variable islate-bound; it allows a method defined in one class to invoke another method that is defined later, in some subclass thereof.

y y y y

C++
C++ is a statically typed , free-form , multi-paradigm , compiled, general purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as an enhancement to the C language. Rules used for designing C++ language: y y y y y y C++ is designed to be a statically typed, general-purpose language that is as efficient and portable as C C++ is designed to directly support multiple programming styles (procedural programming, data abstraction, object-oriented programming, and generic programming) C++ is designed to give the programmer choice, even if this makes it possible for the programmer to choose incorrectly C++ is designed to be as compatible with C as possible, therefore providing a smooth transition from C C++ avoids features that are platform specific or not general purpose C++ is designed to function without a sophisticated programming environment

Sample program
#include <iostream.h>

int main() { cout << "Hello, world!\n"; }

JAVA

Java is a programming language originally Microsystems (now part of Oracle Corporation) in 1995.

developed

by James

Gosling at Sun

The language derives much of its syntax from C and C++ but has a simpler object model and fewerlow-level facilities. Java applications are typically compiled to byte code (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Java is a general-purpose, concurrent, class-based, object-oriented language It is intended to let application developers "write once, run anywhere." Java is currently one of the most popular programming languages in use, particularly for client-server web applications. There were five primary goals in the creation of the Java language: 1. 2. 3. 4. 5. It should be "simple, object-oriented and familiar" It should be "robust and secure" It should be "architecture-neutral and portable" It should execute with "high performance" It should be "interpreted, threaded, and dynamic"

SAMPLE PROGRAM

class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } } SCRIPTING LANGUAGES A high-level programming language that is interpreted by another program at runtime rather than compiled by the computer??s processor as other programming languages (such as C and C++) are. Scripting languages, which can be embedded within HTML, commonly are used to add functionality to a Web page, such as different menu styles or graphic displays or to serve dynamic advertisements. These types of languages are client-side scripting languages,

affecting the data that the end user sees in a browser window. Other scripting languages are serverside scripting languages that manipulate the data, usually in a database, on the server.

PERL (Practical Extraction and Report Language)


Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities without the arbitrary data length limits of many contemporary Unix tools, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities. Perl has become well known for its capabilities in a few areas where it exceeds the capabilities of most other languages. These include String manipulation File handling Regular expressions and pattern matching Flexible arrays (hashes, or associative arrays) VISUAL BASIC SCRIPT (VB Script) VBScript (Visual Basic Scripting Edition) is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. It is designed as a lightweight language with a fast interpreter for use in a wide variety of Microsoft environments. VBScript uses the Component Object Model to access elements of the environment within which it is running. The following are some key points of introduction to the VBScript language. y A procedure is the main construct in VBScript for separating code into smaller modules. VBScript distinguishes between a function, which can return a result in an assignment statement, and a subroutine, which cannot Control structures include the usual iterative and conditional Do Loops, If-Then-Else statements, and Case statements, with some more complex variants, such as Else If and nested control structures. As a memory aid in coding, and certainly for readability, there are a large number of constants, such as True and False for logical values, vbOKCancel and vbYesNo for MsgBox codes, vbBlack and vbYellow for color values, vbCR for the carriage return character, and many others. Variables by default have Variant type.

y y y

User interaction is provided through the functions MsgBox and InputBox which provide a simple dialogue box format for messages and input. For more elaborate GUI interaction with controls, VBScript can be used in combination with HTML, for example, in an HTML Application Names are not case-sensitive.

DECLARATIVE PROGRAMMING Declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow.[1] Many languages applying this style attempt to minimize or eliminate side effects by describing what the program should accomplish, rather than describing how to go about accomplishing it. This is in contrast with imperative programming, which requires an explicitly provided algorithm. Declarative programming often considers programs as theories of a formal logic, and computations as deductions in that logic space. Declarative programming has become of particular interest recently, as it may greatly simplify writing parallel programs. Common declarative languages include those of regular expressions, logic programming, and functional programming.

FUNCTIONAL PROGRAMMING PARADIGM Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming seeks to eliminate the need for program state by expressing all computations in the form of functions that take arguments and return values, without introducing side effects. Those functions read much more like functions in mathematics, where X = X + 1 could never work. In fact, functional programming is based on the lambda calculus, which is a mathematical system for investigating functions. CONCEPTS First-class and higher-order functions Higher-order functions are functions that can either take other functions as arguments or return them as results. The distinction between the two is subtle: "higher-order" describes a mathematical concept of functions that operate on other functions, while "first-class" is a computer science term that describes programming language entities that have no restriction on their use (thus first-class functions can appear anywhere in the program that other first-class entities like numbers can, including as arguments to other functions and as their return values).

Pure functions Purely functional functions (or expressions) have no memory or I/O side effects. This means that pure functions have several useful properties, many of which can be used to optimize the code:
 

If the result of a pure expression is not used, it can be removed without affecting other expressions. If a pure function is called with parameters that cause no side-effects, the result is constant with respect to that parameter list (sometimes called referential transparency), i.e. if the pure function is again called with the same parameters, the same result will be returned (this can enable caching optimizations such as memoization). If there is no data dependency between two pure expressions, then their order can be reversed, or they can be performed in parallel and they cannot interfere with one another (in other terms, the evaluation of any pure expression is thread-safe). If the entire language does not allow side-effects, then any evaluation strategy can be used; this gives the compiler freedom to reorder or combine the evaluation of expressions in a program (for example, using deforestation).

Recursion
Iteration (looping) in functional languages is usually accomplished via recursion. Recursive functions invoke themselves, allowing an operation to be performed over and over. Recursion may require maintaining a stack

Strict versus non-strict evaluation


Functional languages can be categorized by whether they use strict (eager) or non-strict (lazy) evaluation, concepts that refer to how function arguments are processed when an expression is being evaluated Under strict evaluation, the evaluation of any term containing a failing subterm will itself fail. For example, the expression: print length([2+1, 3*2, 1/0, 5-4]) will fail under strict evaluation because of the division by zero in the third element of the list. Under non strict evaluation, the length function will return the value 4, since evaluating it will not attempt to evaluate the terms making up the list. In brief, strict evaluation always fully evaluates function arguments before invoking the function. Non-strict evaluation does not evaluate function arguments unless their values are required to evaluate the function call itself.

Examples LISP (LISt Processing)

LISt Processing". Linked lists are one of Lisp languages' major data structures, and Lisp source code is itself made up of lists. As a result, Lisp programs can manipulate source code as a data structure, giving rise to the macro systems that allow programmers to create new syntax or even new domain-specific languages embedded in Lisp. Lisp (or LISP) is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Features Symbolic expressions Lisp is an expression-oriented language. Unlike most other languages, no distinction is made between "expressions" and "statements"; all code and data are written as expressions

Lists A Lisp list is written with its elements separated by whitespace, and surrounded by parentheses. For example, (1 2 foo) is a list whose elements are three atoms: the values 1, 2, and foo. These values are implicitly typed: they are respectively two integers and a Lisp-specific data type called a "symbol", and do not have to be declared as such. The empty list () is also represented as the special atom nil. This is the only entity in Lisp which is both an atom and a list. Expressions are written as lists, using prefix notation. The first element in the list is the name of a form, i.e., a function, operator, macro, or "special operator" (see below.) The remainder of the list are the arguments. For example, the function list returns its arguments as a list, so the expression (list '1 '2 'foo) Operators Arithmetic operators are treated similarly. The expression (+ 1 2 3 4) evaluates to 10. Arithmetic operators in Lisp are variadic (or n-ary), able to take any number of arguments.

Lambda expressions Another special operator, lambda, is used to bind variables to values which are then evaluated within an expression. This operator is also used to create functions: the arguments to lambda are a list of arguments, and the expression or expressions to which the function evaluates (the returned value is the value of the last expression that is evaluated). The expression (lambda (arg) (+ arg 1))

evaluates to a function that, when applied, takes one argument, binds it to arg and returns the number one greater than that argument. Lambda expressions are treated no differently from named functions; they are invoked the same way Atoms In LISP there were two fundamental data types: y y atoms lists.

A list was a finite ordered sequence of elements, where each element is in itself either an atom or a list, and an atom was anumber or a symbol. A symbol was essentially a unique named item, written as an Alphanumeric string in source code, and used either as a variable name or as a data item in symbolic processing. For example, the list (FOO (BAR 1) 2) contains three elements: the symbol FOO, the list (BAR 1), and the number 2.

Example programs Here are examples of Common Lisp code.

The basic "Hello world" program: (print "Hello world")

Evaluate a number's factorial: (defun factorial (n) (if (<= n 1) 1 (* n (factorial (- n 1)))))

LOGIC BASED PROGRAMMING PARADIGM A logic programming consists of a series of axioms or facts, rules of inference and a theorem or query to be proved. The output is true if the facts support the query and false otherwise. A restricted form of predicate logic wffs, called Horn clauses, can be used as a programming language to solve computing problems as well as database queries. A Horn clause is one of the following forms: 1. Fact: p(c1,...,cn). where p is a predicate symbol (arity=n) and c1, ..., cn are constant symbols. Example: the following are some facts:

eat(bear,fish). eat(bear,fox). eat(fox,rabbit). eat(deer,grass). animal(bear). animal(fish). animal(fox). animal(rabbit). animal(deer). plant(grass). 2. Rule: p :- q1, ..., qk. where p, q1, ..., qk are atomic formulas with constants or variables as arguments. Note: In general, these atomic formulas can have other types of arguments called function terms, but for simplicity we restrict the arguments to be constants or variables. Example: the following are some rules: prey(X) :- eat(Y,X), animal(X). hunted(X) :- prey(X). inFoodChain(X,Y) :- eat(X,Y). inFoodChain(X,Y) :- eat(X,Z), inFoodChain(Z,Y). The rules are interpreted as follows: ( X)( Y)(eat(X,Y) ( X)(prey(X) animal(X) prey(X))

hunted(X)) inFoodChain(X,Y)) inFoodChain(Z,Y) inFoodChain(X,Y))

( X)( Y)(eat(X,Y) ( X)( Y)(eat(X,Z)

i.e. all variables are universally quantified within each rule at the outermost level and the rule itself is treated as an implication. 3. Query: :- q1, ..., qk. where q1, ..., qk are atomic formulas with constants or variables as arguments. Example: the following are some queries: ::::::animal(bear). animal(X). eat(bear,X). eat(X,Y), plant(Y). prey(X). inFoodChain(bear,Y).

Logic programming has many application areas:

y y y y y y y y

Relational Data Bases Natural Language Interfaces Expert Systems Symbolic Equation solving Planning Prototyping Simulation Programming Language Implementation

LOGIC BASED PROGRAMMING PARADIGM A logic programming consists of a series of axioms or facts, rules of inference and a theorem or query to be proved. The output is true if the facts support the query and false otherwise. Example PROLOG Prolog is a general purpose logic programming language associated with artificial intelligence and computational linguistics. Prolog is declarative: the program logic is expressed in terms of relations, represented as facts and rules. A computation is initiated by running a query over these relations. Prolog was one of the first logic programming languages

Everything in Prolog is de ned in terms of two constructs: y y the fact the rule.

A fact is a Prolog statement consisting simply of an identi er followed by an n-tuple of constants. The identi er is interpreted as the name of a (mathematical) relation and the fact states that the speci ed n-tuple is in the relation. In Prolog a relation identi er is referred to as a predicate; when a tuple of values is in a relation we say the tuple satis es the predicate

RECURSIVE PROGRAMMING
A programming technique called Recursive Programming, in which a procedure calls itself repeatedly until some escape condition is met. Recursive programming is a powerful technique that can greatly simplify some programming tasks. In summary, recursive programming is the situation in which a procedure calls itself, passing in a modified value of the parameter(s) that was passed in to the current iteration of the procedure. Typically, a recursive programming environment contains (at least) two procedures: first, a procedure to set up the initial environment and make the initial call to the recursive procedure, and second, the recursive procedure itself that calls itself one or more times. Sample example The Factorial of a number N is the product of all the integers between 1 and N. The factorial of 5 is equal to 5 * 4 * 3 * 2 * 1 = 120. In the real world you would not likely use a recursive procedure for this, but it will serve as a simple yet illustrative example. The first procedure is named DoFact sets things up, calls the Fact function and displays the result. Sub DoFact() Dim L As Long Dim N As Long N=3 L = Fact(N) Debug.Print "The Factorial of " & CStr(N) & " is " & Format(L, "#,##0") End Sub The Fact function does the real work of calculating the factorial. Function Fact(N As Long) As Long If N = 1 Then Fact = 1 Else Fact = N * Fact(N - 1) End If End Function

In this code, the value of the input N is tested. If it is 1, the function simply returns 1. If N is greater than 1, Fact calls itself passing itself the value N-1. The function returns as its result the input value N times the value of itself evaluated for N-1.

LOGIC BASED PROGRAMMING PARADIGM A logic programming consists of a series of axioms or facts, rules of inference and a theorem or query to be proved. The output is true if the facts support the query and false otherwise. A restricted form of predicate logic wffs, called Horn clauses, can be used as a programming language to solve computing problems as well as database queries. A Horn clause is one of the following forms: 4. Fact: p(c1,...,cn). where p is a predicate symbol (arity=n) and c1, ..., cn are constant symbols. Example: the following are some facts: eat(bear,fish). eat(bear,fox). eat(fox,rabbit). eat(deer,grass). animal(bear). animal(fish). animal(fox). animal(rabbit). animal(deer). plant(grass). 5. Rule: p :- q1, ..., qk. where p, q1, ..., qk are atomic formulas with constants or variables as arguments. Note: In general, these atomic formulas can have other types of arguments called function terms, but for simplicity we restrict the arguments to be constants or variables. Example: the following are some rules: prey(X) :- eat(Y,X), animal(X). hunted(X) :- prey(X). inFoodChain(X,Y) :- eat(X,Y). inFoodChain(X,Y) :- eat(X,Z), inFoodChain(Z,Y). The rules are interpreted as follows: ( X)( Y)(eat(X,Y) animal(X) prey(X))

( X)(prey(X)

hunted(X)) inFoodChain(X,Y)) inFoodChain(Z,Y) inFoodChain(X,Y))

( X)( Y)(eat(X,Y) ( X)( Y)(eat(X,Z)

i.e. all variables are universally quantified within each rule at the outermost level and the rule itself is treated as an implication. 6. Query: :- q1, ..., qk. where q1, ..., qk are atomic formulas with constants or variables as arguments. Example: the following are some queries: ::::::animal(bear). animal(X). eat(bear,X). eat(X,Y), plant(Y). prey(X). inFoodChain(bear,Y).

Logic programming has many application areas:


y y y y y y y y

Relational Data Bases Natural Language Interfaces Expert Systems Symbolic Equation solving Planning Prototyping Simulation Programming Language Implementation

You might also like