Programming Languages and Paradigms Introduction Definitions n Programming

  • Slides: 19
Download presentation
Programming Languages and Paradigms Introduction

Programming Languages and Paradigms Introduction

Definitions n Programming Language n notation for specifying programs/computations consists of words, symbols, and

Definitions n Programming Language n notation for specifying programs/computations consists of words, symbols, and rules for writing a program Programming Paradigm n n n 11/24/2020 programming “technique” way of thinking about programming view of a program Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 2

Programming Paradigms n Imperative Programming n n Object-Oriented Programming n n program as a

Programming Paradigms n Imperative Programming n n Object-Oriented Programming n n program as a collection of classes for interacting objects Functional Programming n n program as a collection of statements and procedures affecting data (variables) program as a collection of (math) functions Others 11/24/2020 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 3

Some Languages by Paradigm n Imperative (also called Structured or Procedural) Programming n n

Some Languages by Paradigm n Imperative (also called Structured or Procedural) Programming n n Object-Oriented Programming n n FORTRAN, BASIC, COBOL, Pascal, C Small. Talk, C++, Java Functional Programming n 11/24/2020 LISP, ML, Haskell Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 4

History of Languages n 1950 s to 1960 s n n 1960 s to

History of Languages n 1950 s to 1960 s n n 1960 s to 1970 s n n (ALGOL-based) Pascal and others 1970 s to 1980 s n n FORTRAN, COBOL, LISP, BASIC Prolog, C, Ada 1980 s to 1990 s n 11/24/2020 C++, ML, Perl, Java Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 5

Paradigm Change n n n For example, from Procedural to Object. Oriented Programming Arises

Paradigm Change n n n For example, from Procedural to Object. Oriented Programming Arises from problems encountered in one paradigm but addressed in another Case study: from C to C++ n n 11/24/2020 Evolution from procedural, to modular, to objectbased, to object-oriented programming Stroustrup book section 1. 2 (2 nd edition, pp. 14 -22): required reading material Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 6

Case Study: Stacks n Stack n n n last-in, first-out structure operations: push, pop

Case Study: Stacks n Stack n n n last-in, first-out structure operations: push, pop Stacks are used to support some solution n n 11/24/2020 push and pop are defined and implemented as functions the solution consists of code that invoke these functions Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 7

Implementing a Stack n Stack can be implemented as an array n n Or

Implementing a Stack n Stack can be implemented as an array n n Or as a linked list n n array contains pushed elements an integer refers to top of the stack most common implementation using pointers and dynamic allocation Other implementations 11/24/2020 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 8

Array Implementation in C char Store[MAX]; int top = 0; void push(char x) {

Array Implementation in C char Store[MAX]; int top = 0; void push(char x) { if (top < MAX) Store[top++] = x; else printf(“fulln”); } 11/24/2020 char pop() { if (top > 0) return Store[--top]; else printf(“emptyn”); }. . . Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 9

Using the Stack void application() { … push(‘x’); … result = pop(); … }

Using the Stack void application() { … push(‘x’); … result = pop(); … } 11/24/2020 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 10

Procedural Programming n Focus is on writing good functions and procedures n n use

Procedural Programming n Focus is on writing good functions and procedures n n use the most appropriate implementation and employ correct efficient algorithms Stack example (assume array implementation) n n n 11/24/2020 one source file Store and top are global variables stack and application functions defined at the same level (file) Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 11

Problems n Application can alter implementation details n n n can directly manipulate top

Problems n Application can alter implementation details n n n can directly manipulate top and Store from application() integrity of stack not ensured Stack code and application code are not separated 11/24/2020 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 12

Encapsulation and Modular Programming n Focus is on writing good modules n n n

Encapsulation and Modular Programming n Focus is on writing good modules n n n hide implementation details from user provide an interface Stack example n n n 11/24/2020 stack. h contains prototypes for push, pop stack. c contains stack code, Store and top declared static (local to stack. c) application includes stack. h Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 13

Benefits from Modules n n n Application cannot destroy the integrity of the stack

Benefits from Modules n n n Application cannot destroy the integrity of the stack Stack implementation can change without affecting application source Question: what happens if we need more than one stack? 11/24/2020 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 14

Multiple Stacks n Strategy 1 (use structs) n n n in stack. h, define

Multiple Stacks n Strategy 1 (use structs) n n n in stack. h, define a stack structure that contains Store and top; push, pop now have an extra parameter that specifies which stack application code defines stack variables Strategy 2 (use handles) n n 11/24/2020 implement multiple data structures in stack. c use an integer (the handle) to specify “stack number” Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 15

Modules and Multiple Stacks n Disadvantage of strategy 1: n n n implementation (data)

Modules and Multiple Stacks n Disadvantage of strategy 1: n n n implementation (data) is exposed back to original problem on stack integrity Disadvantage of strategy 2: n n 11/24/2020 stack module will be unnecessarily complex handle is artificial (what if an arbitrary integer is passed? ) Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 16

Abstract Data Types and Object-based Programming n Focus is on writing good classes (or

Abstract Data Types and Object-based Programming n Focus is on writing good classes (or types) that define operations on objects of the class n n n class defined like a module (encapsulation enforced) but multiple instances now possible user-defined type Stack example (C++) n 11/24/2020 stack. h and stack. cpp define a stack class Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 17

Object-Oriented Programming n n n Incorporates both encapsulation and inheritance through the class concept

Object-Oriented Programming n n n Incorporates both encapsulation and inheritance through the class concept Focus is on writing good classes and on code reuse Examples n n 11/24/2020 Shape, Circle, and Rectangle in a drawing program Employee, Faculty, Staff in a university personnel system Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 18

What’s Next? n n n Survey of languages by paradigm Discussion of language features

What’s Next? n n n Survey of languages by paradigm Discussion of language features and language design decisions Related areas: language implementation, translation, syntax, semantics 11/24/2020 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 19