Programming Paradigms                               Procedural Programming Paradigm
> A programming paradigm is a style, or “way,”      > The procedure is the principal building block
of programming.                                     of the program
> Some languages make it easy to write in           > The states of computation are represented by
some paradigms but not others.                      the stack
> A paradigm is a way of doing something (like      > No restriction, recursion is allowed
programming).                                       > It relies on procedures or subroutines
> “pattern for modeling and solving problems”       Examples of Languages that Support
> “the way we think of problems and how they        Imperative Paradigm
are going to be solved                                    C: developed by Dennis Ritchie and
                                                             Ken Thompson
Imperative/Structured Paradigm                            C++: developed by Bjarne Stroustrup
> based on the Von Neumann architecture                   Java: developed by James Gosling at
where data and programs are stored in the                    Sun Microsystems
same memory.                                              Pascal: developed by Niklaus Wirth
> central features are:                             Object Oriented Paradigm
    variables (model the memory cells)             >Data Oriented
    assignment statements (based on piping         > paradigm in which real-world objects are
       operation)                                   each viewed as separate entities
    iteration form of repetition (most efficient   >written as a collection of classes and object
       method).                                     which are meant for communication.
Examples of Languages that Supports                 >The smallest and basic entity is object and all
Imperative Paradigm                                 kind of computation is performed on the objects
    C: developed by Dennis Ritchie and Ken         only.
       Thompson                                     >emphasis is on data rather procedure.
    Fortran: developed by John Backus for          Features of Object-Oriented Paradigm
       IBM                                          ▪ Encapsulate data objects with processing
    Basic: developed by John G Kemeny              ▪ Inheritance and dynamic type binding
       and Thomas E Kurtz                           ▪ Grew out of imperative languages
                                                    ▪ Objects are organized into classes, from
Advantages of Imperative Languages                  which they inherit methods and equivalent
    Efficient                                      variables
    Popular                                        ▪ The object-oriented paradigm provides key
    Familiar                                       benefits of reusable code and code
                                                    extensibility.
Disadvantages of Imperative Languages               Characteristics of Object-Oriented
    The semantics of a program can be              Programming
      complex to understand or prove,               ▪ Data as well as operations are encapsulated
      because of referential transparency           in objects
      does not hold (due to side effects)           ▪ Information hiding is used to protect internal
                                                    properties of an object
    Abstraction is more limited than with
                                                    ▪ Objects interact by means of message
      some paradigms;
                                                    passing
    Order is crucial, which doesn't always
                                                    ▪ In most object-oriented languages objects are
      suit itself to problems.
                                                    grouped in classes
      Objects in classes are similar enough to   ▪ In logical programming, the main emphasize
       allow programming of the classes, as       is on knowledge base and the problem.
       opposed to programming of the              ▪ The execution of the program is very much
       individual objects                         like proof of mathematical statement
     Classes represent concepts whereas          ▪ It would solve logical problems like puzzles,
       objects represent events                   series etc
Advantages of Object-Oriented Paradigm            Characteristics of Logic Paradigm
     Data security                               ▪ Discipline and idea - Automatic proofs within
     Inheritance                                 artificial intelligence
     Code reusability o Flexible and             ▪ Based on inference rules, and queries.
       abstraction is also present                ▪ Program execution becomes a systematic
Examples of Languages that Support Object         search in a set of facts, making use of a set of
Oriented Paradigm                                 inference rules
▪ Simula: first OOP language                      ▪ The Logical Paradigm takes a declarative
▪ Java: developed by James Gosling at Sun         approach to problem-solving.
Microsystems                                      ▪ Various logical assertions about a situation
▪ C++: developed by Bjarne Stroustrup             are made, establishing all known facts. Then
▪ Objective-C: designed by Brad Cox               queries are made. The role of the computer
▪ Visual Basic .NET: developed by Microsoft       becomes maintaining data and logical
▪ Python: developed by Guido van Rossum           deduction.
▪ Ruby: developed by Yukihiro Matsumoto           Advantages of Logical Paradigm
▪ Smalltalk: developed by Alan Kay, Dan                 The system solves the problem, so the
Ingalls, Adele Goldberg                                    programming steps themselves are kept
                                                           to a minimum.
Parallel Processing Approach                            Proving the validity of a given program is
▪ the processing of program instructions by                simple.
dividing them among multiple processors.
▪ possess many numbers of processor with the      Functional Paradigm
objective of running a program in less time by    ▪ Views all subprograms as functions in the
dividing them.                                    mathematical sense.
▪ seems to be like divide and conquer             ▪ They take in arguments and return a single
                                                  solution.
Logic Paradigm                                    ▪ The solution returned is based entirely on the
▪ The logic paradigm fits extremely well when     input, and the time at which a function is called
applied in problem domains that deal with the     has no relevance.
extraction of knowledge from basic facts and      ▪ The computational model is therefore one of
relations.                                        function application and reduction.
▪ The logical paradigm seems less natural in      ▪ Functions are treated as data, assumed to
the more general areas of computation.            have the value of what they return.
▪ It can be termed as abstract model of           ▪ They can be passed to other functions as
computation                                       arguments or returned from functions.
▪ In logic programming we have a knowledge        ▪ Permit functional solutions to problems by
base which we know before and along with the      permitting a programmer to treat functions as
question and knowledge base which is given to     first-class objects
machine, it produces result.
▪ Operate only through functions which return      Variables
one value given a list of parameters.              ▪ The abstractions in a language for the
▪ Functions themselves are considered values       memory cells of the machine
that can be passed to other functions              ▪ characterized by a collection of properties, or
                                                   attributes
Advantages of Functional Paradigm                       Type (the most important)
▪ High level of abstraction, especially when            Scope
functions are used, suppresses many of the
details of programming and thus removes the        Names
possibility of committing errors.                  ▪ Names are also associated with
▪ The lack of dependence on assignment             subprograms, formal parameters, and other
operations, allowing programs to be evaluated      program constructs.
in many different orders.                          ▪ Identifier  Name
▪ The absence of assignment operations
makes the function-oriented programs much          Design Issues
more amenable to mathematical proof and            ▪ Are names case sensitive?
analysis than are imperative programs,             ▪ Are the special words of the language
because functional programs possess                reserved words or keywords?
referential transparency.
                                                   Name Forms
Disadvantages of Functional Paradigm               ▪ A name is a string of characters used to
▪ Perhaps less efficient                           identify some entity in its names
▪ Problems involving many variables, or a lot of           ▪ Length limitations are different for
sequential activity are sometimes easier to                different languages
handle imperatively or with object-oriented        ▪ Naming convention
programming.                                               ▪ Underscore characters
                                                           ▪ Camel notation
Example of Languages that Support Functional               ▪ Pascal notation
Paradigm                                           ▪ Case sensitive
▪ JavaScript: developed by Brendan Eich
▪ Haskwell: developed by Lennart Augustsson,       Variables
Dave Barton                                        ▪ A program variable is an abstraction of a
▪ Scala: developed by Martin Odersky               computer memory cell or collection of cells.
▪ Erlang: developed by Joe Armstrong, Robert       ▪ A variable can be characterized as a sextuple
Virding                                            of attributes:
▪ Lisp: developed by John Mccarthy                         o (Name, address, type, lifetime, and
                                                           scope)
Database/Data Driven Paradigm
▪ A database program is the heart of a business    Name
information system and provides file creation,     ▪ Identifier
data entry, update, query and reporting            ▪ Most variables have names
functions.                                                 o Variables without names
▪ There are several programming languages                         ▪ Temporary variables – E.g.
that are developed mostly for database                            x=y*z+3
application.
                     » The result of y*z may be     allocated object to a pointer variable, or the
                     stored in a temporary          binding of a virtual method to a function call
                     variable
                                                    Binding
The Concept of Binding                              ▪ Static binding – Occurs before run time
▪ A binding is an association between an            begins and remains unchanged throughout
attribute and an entity                             program execution
        o A variable and its type or value          ▪ Dynamic binding – Occurs during run time
        o An operation and symbol                   or can change in the course of program
▪ Binding time                                      execution
        o The time at which a binding takes
        place                                       Type Bindings
                                                    ▪ Before a variable can be referenced in a
Binding Take Place                                  program, it must be bound to a data type
1. Language design time: The syntax and
    semantics of a language are typically set at    Static binding
    language design time.                           ▪ Resolved at compile time by compiler
Examples would be the control constructs, how       ▪ Also called as early binding
variable declarations are written, whether static   ▪ Binding happens before a program run
or dynamic scoping is used.
2. Language implementation time: Most               Dynamic binding
    language manuals leave a variety of issues      ▪ When a compiler is not able to resolve the
    to the discretion of the language               binding at compile time
    implementor.                                    ▪ Also called as late binding
Typical examples might be the precision             ▪ Binding happens during runtime
(number of bits) of the fundamental types, the
organization and maximum sizes of the stack         Scope
and the heap, and the handling of run-time          ▪ The scope of a variable is the range of
exceptions, such as arithmetic overflow.            statements in which the variable is visible.
3. Compile time: Compiler associates names                 o A variable is visible in a statement if it
with objects, for example, a type with a variable          can be referenced in that statement.
or the layout of a stack allocated object with a    ▪ Local & non local variables
variable.
4. Link time: References to functions may not       Local Scope
be resolved until separate. o files are compiled    > Local variables are those variables defined in
together. Then a placeholder reference can be       a function, which includes the function's
replaced with an "address" (actually an offset      parameters.
from a relocation register)                         > Parameters are initialized by the values
5. Load time: A program is actually assigned        passed in as a part of the function call.
physical memory when it executes
6. Run time: Binding is delayed until run-time,     Static Scope
such as dynamic typing in interpreted               ▪ The scope of a variable can be statically
languages (the type of a variable cannot be         determined o Prior to execution
determined until it is assigned a value in          ▪ Memory allocation and initialization summary
dynamic typing), the assignment of a heap-
Global Scope                                          Sequencing - most basic control flow
▪ always visible from the point of definition to      -it refers to doing elaborations, evaluations, or
the end of the file in which they are defined.        executions one after another.
▪ Global variables are always visible from the        -The execution of statements and evaluation of
point of definition to the end of the file in which   expressions is usually in the order in which
they are defined. They are typically used in          they appear in a program text In a computer
three situations:                                     program or an algorithm, sequence involves
       ▪ Whenever two or more functions must          simple steps which are to be executed one
       share data                                     after the other.
       ▪ When a function creates or uses a very
       large amount of discrete data (discrete        Selection- testing a condition.
       in the sense that it is not contained
       within a single data structure) that is not    *All the paths from a selection control structure
       easily passed to and/or returned from          join back up at the end of the control structure,
       the function. Structured data such as          before moving on to the next lines of code in a
       arrays, structures, and classes greatly        program.
       reduce this use of global data                 * A question using Boolean concepts usually
       ▪ When data is passed to and/or                controls which path is selected.
       returned from functions that are               * A question using Boolean concepts usually
       separated by many function calls where         controls how often the loop will execute.
       the intervening functions do not use the
       data. The next figure illustrates this         Run-Time Condition- determines the choice
       situation.                                     among two or more statements or expressions
Modules and File Scope                                Two-Way Selection or Multi-Way Selection -
▪ Variables that are defined in one file can be       able to select between two or more alternate
declared in another file with the extern              paths.
keyword. To easily make a variable truly global,
declare the variable in a header file                 Binary Branch -occurs because of testing a
                                                      condition whose answer is either 'Yes' or 'No
▪ File scope variables are often called global        Multiple Branching can also occur
variables because they are easily made global         *CASE statement is used to set up this control
                                                      structure. In pseudocode a CASE Statement
                                                      looks like this.
                                                      Iteration -process of Looping.
                                                      - be able to repeat some lines of code
                                                      -accomplished with test before loops, test after
Control flow- order function calls, instructions,     loops, and counting loops.
and statements are executed or evaluated
when a program is running.                            Loop structures
                                                       -can be created in a variety of ways.
Control flow statements- determine what               *The visual display of iteration creates a
section of code is run in a program at any time.      circular loop pattern when flowcharted, thus the
word “loop” is associated with iteration control
structures.
Pre-Test Loop – Guarded Loop or a WHILE
Loop
-condition must be met at the very beginning of
the loop.
-The segment of the program will not be
executed if the condition is not met.
- The body of the loop is executed repeatedly
while the condition is true.
Post-test Loop
-executes the body of the loop at least once
before the termination condition can be met.
-The body of the loop is repeatedly executed
until the termination condition is true.
Counted Loop – Counted Loop or FOR
NEXT Loop
-know exactly how many times you want
something to repeat you can set up a structure
Modularization
- process of breaking a program solution up
into Subprograms
-end up with a mainline program showing
subprogram names and several subprograms
which provide more detail.
*Each subprogram then has its own solution
starting with BEGIN SUBPROGRAM followed
by the underlined title used in the main
program
*The end of the subprogram is marked by the
keywords END SUBPROGRAM