Overview of Programming Paradigms Lecture Objectives Be able

Overview of Programming Paradigms Ø Lecture Objectives: Ø Be able to explain the differences between programming languages and programming paradigms. Ø Be able to differentiate between low-level and high-level programming languages and their associated advantages and disadvantages Ø Be able to list four programming paradigms and describe their strengths and weaknesses. Ø Introduction to Computer Programming Ø Programming Languages Ø Programming Paradigms Ø Exercises 1

Computer Programming 1. The functions of a computer system are controlled by computer programs 2. A computer program is a clear, step-by-step, finite set of instructions 3. A computer program must be clear so that only one meaning can be derived from it, Ø A computer program is written in a computer language called a programming language 2

Programming Languages Ø There are three categories of programming languages: Ø 1. Machine languages. Ø 2. Assembly languages. Ø 3. High-level languages. Ø Machine languages and assembly languages are also called low-level languages 3

Programming Languages (cont’d) Ø A Machine language program consists of a sequence of zeros and ones. Ø Each kind of CPU has its own machine language. Ø Advantages Ø Fast and efficient Ø Machine oriented Ø No translation required Ø Disadvantages Ø Not portable Ø Not programmer friendly 4

Assembly Language Ø Assembly language programs use mnemonics to represent machine instructions Each statement in assembly language corresponds to one statement in machine language. Ø Assembly language programs have the same advantages and disadvantages as machine language programs. Ø Compare the following machine language and assembly language programs: Ø 8086 Machine language program for var 1 = var 1 + var 2 ; 8086 Assembly program for var 1 = var 1 + var 2 ; 1010 0001 0000 MOV AX , var 1 0000 0011 0000 0110 0000 0010 ADD AX , var 2 1010 0011 0000 MOV var 1 , AX 5

High-Level Programming Languages Ø A high-level language (HLL) has two primary components Ø (1) a set of built-in language primitives and grammatical rules Ø (2) a translator Ø A HLL language program consists of English-like statements that are governed by a strict syntax. Ø Advantages Ø Portable or machine independent Ø Programmer-friendly Ø Disadvantages Ø Not as efficient as low-level languages Ø Need to be translated Ø Examples : C, C++, Java, FORTRAN, Visual Basic, and Delphi. 6

Programming Paradigms Ø Why are there hundreds of programming languages in use today? Ø Some programming languages are specifically designed for use in certain applications. Ø Different programming languages follow different approaches to solving programming problems Ø A programming paradigm is an approach to solving programming problems. Ø A programming paradigm may consist of many programming languages. Ø Common programming paradigms: Ø Ø Imperative or Procedural Programming Object-Oriented Programming Functional Programming Logic Programming 7

Programming Paradigms: Imperative Ø In this paradigm, a program is a series of statements containing variables. Ø Program execution involves changing the memory contents of the computer continuously. Ø Example of imperative languages are: C, FORTRAN, Pascal, COBOL etc Ø Advantages Ø low memory utilization Ø relatively efficient Ø the most common form of programming in use today. ØDisadvantages Ødifficulty of reasoning about programs Ødifficulty of parallelization. ØTend to be relatively low level. 8

Programming Paradigms: Object-Oriented Ø A program in this paradigm consists of objects which communicate with each other by sending messages Ø Example object oriented languages include: Java, C#, Smalltalk, etc Ø Advantages Ø Conceptual simplicity Ø Models computation better Ø Increased productivity. ØDisadvantages ØCan have a steep learning curve, initially ØDoing I/O can be cumbersome 9

Programming Paradigms: Functional Ø A program in this paradigm consists of functions and uses functions in a similar way as used in mathematics Ø Program execution involves functions calling each other and returning results. There are no variables in functional languages. Ø Example functional languages include: ML, Miranda. TM, Haskell Ø Advantages Ø Small and clean syntax Ø Better support for reasoning about programs Ø They allow functions to be treated as any other data values. Ø They support programming at a relatively higher level than the imperative languages Ø Disadvantages ØDifficulty of doing input-output ØFunctional languages use more storage space than their imperative cousins 10

Programming Paradigms: Logic Ø A program in the logic paradigm consists of a set of predicates and rules of inference. Ø Predicates are statements of fact like the statement that says: water is wet. Ø Rules of inference are statements like: If X is human then X is mortal. Ø The predicates and the rules of inference are used to prove statements that the programmer supplies. Ø Example: Prolog Ø Advantages Ø Good support for reasoning about programs Ø Can lead to concise solutions to problems Ø Disadvantages Ø Slow execution Ø Limited view of the world Ø That means the system does not know about facts that are not its predicates and rules of inference. Ø Difficulties in understanding and debugging large programs 11

Which Programming Paradigm is Best? Ø Which of these paradigms is the best? Ø The most accurate answer is that there is no best paradigm. Ø No single paradigm will fit all problems well. Ø Human beings use a combination of the models represented by these paradigms. Ø Languages with features from different paradigms are often too complex. Ø So, the search of the ultimate programming language continues! 12

Review Questions 1. List two advantages and two disadvantages of low-level languages. 2. Explain the similarities and differences between an assembly language and a machine language. 3. Mention the programming paradigm to which each of the following languages belongs: Visual Basic, Java, C#, Haskell, Lisp, Prolog, Pascal. 4. Which programming paradigms give better support for reasoning about programs? 5. Which programming paradigms give better support for doing I/O? 13
- Slides: 13