The Imperative Paradigm

Similar documents
The Object Oriented Paradigm

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar

CSE 307: Principles of Programming Languages

Chapter1 Overview of computers

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Crash Course into. Prof. Dr. Renato Pajarola

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Functions. Systems Programming Concepts

Summary of basic C++-commands

CS201 Some Important Definitions

OUTLINE. Review Functions Pointers Function calls Array Pointers Pointer Arrays Data Structures and dynamic Memory Function Pointers quicksort example

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Computer Science & Engineering 150A Problem Solving Using Computers

Data Structures in C++ Using the Standard Template Library

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

CSCI312 Principles of Programming Languages!

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

C++ Programming Lecture 11 Functions Part I

Scientific Computing

The University of Aizu School of Computer Science and Engineering Introduction to Programming. Course Syllabus (Special Track for Foreign Students)

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Beginning C Programming for Engineers

C Programs: Simple Statements and Expressions

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Programming and Data Structure

Engineering Problem Solving with C++, Etter/Ingber

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

Short Notes of CS201

22c:111 Programming Language Concepts. Fall Types I

PROGRAMMING IN C AND C++:

CS201 - Introduction to Programming Glossary By

Computers Programming Course 6. Iulian Năstac

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

CSC 533: Organization of Programming Languages. Spring 2005

Object Oriented Paradigm

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Arrays, Pointers and Memory Management

Chapter 2. Outline. Simple C++ Programs

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

COEN244: Class & function templates

Chapter 4: Basic C Operators

The von Neumann Architecture. IT 3123 Hardware and Software Concepts. The Instruction Cycle. Registers. LMC Executes a Store.

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Ch. 7: Control Structures

HANDLING NONLOCAL REFERENCES

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

VALLIAMMAI ENGINEERING COLLEGE

Chapter 15 - C++ As A "Better C"

Chapter 1: Object-Oriented Programming Using C++

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Structure of this course. C and C++ Past Exam Questions. Text books

Chapter 9. Subprograms

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Chapter 2 - Control Structures

from Appendix B: Some C Essentials

Object Oriented Programming

Introduction to C++ Introduction to C++ 1

Partha Sarathi Mandal

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Problem Solving & C M. Tech. (CS) 1st year, 2014

VALLIAMMAI ENGINEERING COLLEGE SRM NAGAR, KATTANGULATHUR

Subject: Fundamental of Computer Programming 2068

Introduction to Python, Cplex and Gurobi

Chapter 6 Control Flow. June 9, 2015

Why learn Computer Programming? Computer-based problem solving in Science and Engineering. Why learn Fortran? Elementary Computer Organization

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

1 Introduction to C part I

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

Personalised Learning Checklist ( ) SOUND

CSE123. Program Design and Modular Programming Functions 1-1

Part I Basic Concepts 1

Home Works and Assignments

Expressions. Eric McCreath

CT 229 Java Syntax Continued

CS558 Programming Languages

Come and join us at WebLyceum

Functional Programming. Big Picture. Design of Programming Languages

Chapter 6 Single-dimensional Arrays

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

CS 314 Principles of Programming Languages

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

WELCOME! (download slides and.py files and follow along!) LECTURE 1

1 P age DS & OOPS / UNIT II

M.C.A DEGREE EXAMINATION,NOVEMBER/DECEMBER 2010 Second Semester MC 9222-OBJECT ORIENTED PROGRAMMING (Regulation 2009)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Transcription:

The Imperative Paradigm Joseph Spring 7COM1023 Programming Paradigms 1

Discussion What makes a Language Imperative? Procedural Abstraction Expressions and Assignment Library Support for Data Structures Imperative Programming and C (Next Week) 2

The Imperative Paradigm Said to be the oldest and most well developed programming paradigm Emerged with the first computers (1940 s) It s elements directly reflect the architectural characteristics of modern computers In mid 1940 s it was recognised (von Neumann et al) that a program and it s data could reside in main memory consistent with Turing s research in 1936 Led to an enormous increase in the potential power and versatility of Computers Previously computers had stored their programs outside of main memory using a wire and plug board 3

The Imperative Paradigm The architecture (von Neumann Eckert Model) is the basis for the imperative paradigm Memory contains: Program Instructions (the Program Store) Data values (the Data Store) At the heart of this architecture: Is the idea of assignment changing the value of a memory location and destroying it s previous value sum = sum + num; All imperative languages include assignment as a central concept 4

The Imperative Paradigm In addition to the central idea of assignment, the imperative paradigm supports: Variable declarations Expressions Conditional statements Loops Procedural abstraction 5

The Imperative Paradigm Variable declarations Assign names to memory locations Associate types with stored values Expressions Interpreted by: retrieving the current value of named variables from their respective memory locations Computing a result from the above values Given any reference to a variable x, the memory returns the current value in the location associated with x 6

The Imperative Paradigm Conditional statements and Loops Commands normally executed in the order that they appear in memory However: Conditional and unconditional branching statements can interrupt this normal flow of execution Due to the extensive use of branching early imperative programs were often modelled using flowcharts Exercise Construct a flowchart to compute Fibonacci Numbers 7

Originally: The Imperative Paradigm Commands in imperative languages were simple abstractions of instructions in standard von Neumann Eckert machines These included: Assignment statements Provide ability to dynamically update value stored in memory location Conditional statements and Branching statements If combined allow statements to be skipped or repeatedly executed 8

Turing Complete Definition - An imperative language is said to be Turing Complete if it provides an effective basis for implementing any algorithm that can be designed Imperative languages containing: Integer variables and values Basic arithmetic operations Assignment statements Memory based statement sequencing Conditionals and Branching statements Are said to be Turing Complete 9

Turing Complete We Note: Excessive use of the Branching statement (the go to statement) was considered harmful to the development of reliable programs (Dijkstra 1968) It may be shown that any statement sequence in an imperative program that includes a branching statement can be written equivalently using only conditionals and while loops (Böhm-Jacopini theorem 1966) 10

Imperative Programming Language An imperative programming language is: one which is Turing Complete and supports certain common features that have emerged with the development of the imperative paradigm Control structures Input/output Error and exception handling Procedural abstraction Expressions and assignment Library support for data structures These features appear in a variety of languages 11

Procedural Abstraction In the Imperative Programming Paradigm programs are modelled as Algorithms plus Data Structures (Wirth 1976) Algorithms are developed into programs using two complementary ideas Procedural Abstraction Stepwise Refinement Definition Procedural abstraction allows programmer to be concerned with the interface between functions and what they compute Ignoring details of how computations are achieved 12

Stepwise Refinement Definition (Stepwise refinement) utilises procedural abstraction by developing an algorithm from its most general form into a specific implementation Example (Sorting Function) Program requires algorithm to sort an array of numbers while ignoring details of how sort is to be achieved This has the advantage that any given implementation of the sorting algorithm can be replaced without affecting the original interface 13

Stepwise Refinement Example (Sorting Function - continued) Interface for sort routine is of the form: sort( list, len) where list denotes the array of numbers to be sorted and len contains the number of numbers in the list First approximation to the function: for each i in the sequence of indices of list { list[i] = minimum element in remainder of list } 14

Stepwise Refinement Second approximation to the function: for each i in the sequence of indices of list { for each j > i in the sequence of indices of list { list[i], list[j] = min, max of list[i], list[j] } } 15

Stepwise Refinement Third (possible) approximation to the function: for each i in the sequence of indices of list { for each j > i in the sequence of indices of list { if list[j] < list[i] { swap (list{i}, list{j} } } } 16

Stepwise Refinement The third (possible) approximation is now in a form that can be easily coded in an imperative language. Example (C style encoding) void sort(type list, int len){ for ( int i = 0; i < len; i++) if (list[j] < list[i] ) { Type t = list[j]; list[j] = list[i]; list[i] = t; } } 17

Expressions and Assignment Fundamental to all imperative languages the assignment statement General form (most popular): target = expression target := expression (Fortran style) (Algol style) Example sum = sum + count; Semantics (referred to as copy semantics) involves: Expression is evaluated to a particular value Value is copied to the Target Expressions involve standard math and logic operators, with calls to standard functions provided by language library 18

Expressions and Assignment Some standard mathematical functions Function Fortran C/C++ Sine of x sin(x) sin(x) Cosine of x cos(x) cos(x) Tangent of x tan(x) tan(x). Arcsine of x asin(x) asin(x) x y X**y pow(x,y). Note: in general the Fortran operators are a subset of the set of C operators which are a subset of the set of C++ operators 19

Expressions and Assignment Some standard function libraries in C Functions Provided Mathematical functions Character classification functions String manipulation functions Utility functions Date and time functions C Library math.h ctype.h string.h stdlib.h time.h 20

Library Support for Data Structures The basic data structures in the imperative paradigm are arrays and records (also known as a structure). The following example is from C: struct employeetype { }; int id; char name[26]; int age; float salary; char dept; struct employeetype employee; 21

Library Support for Data Structures Every field within a structure can be a different data type Every field name within a structure must be unique Assignments are achieved by statements of the form: employee.age = 45; employee being a struct containing the int field age Libraries Extensive libraries of useful functions are to be found in modern programming languages Allow programmers to reuse useful code for functions or data structures 22

Library Support for Data Structures Standard C++ library contains following main parts: Functions for defining and manipulating data structures Input /Output functions A string class A complex number class Framework for fitting programs to execution environments E.g. implementation details for each elementary data type on a particular architecture Memory allocation and de-allocation functions Exception handling functions A class optimised for matrix arithmetic 23

Library Support for Data Structures Prior to the current C++ library being adopted in late 1990 s The parts described on the previous slide were known as the Standard Template Library (STL) This portion of the Standard C++ library contains data structures and functions to manipulate the data structures The library may therefore be considered as being designed for imperative rather than OO programming Programmers that prefer the imperative rather than the OOP can easily use the functionality offered by the standard C++ library 24

Library Support for Data Structures Main elements provided by Standard C++ Library Iterators Vectors Lists Stacks, queues, dequeues and priority queues Sets and Multisets Maps Graphs Strings Complex numbers Overloading Generics 25

Library Support for Data Structures Some major functions used to manipulate data structures Subscripting a vector Resizing vectors and lists Inserting and removing elements from vectors, lists and maps Vector, list, set, map, graph and string searching Vector and list sorting List, set and map insertion and deletion Queue and stack operations (push, pop, ) Graph functions (shortest path, ) String functions (substring, insertion, ) Arithmetic, comparison, and I/O functions for complex numbers (See Josuttis, 1999) 26

Imperative Programming and C Next week 27

References 1) Dijkstra E. W., Go to statement considered harmful, Communications of ACM, 11, pp 147-148, (March) 1968 2) Turing A. M., On Computable Numbers, with an application to the Entscheidungsproblem, Proceedings of the London Mathematical Society 2, p265. A correction: 43, pp544-546, 1936 3) Wirth N, Algorithms + data Structures = Programs, Prentice Hall, 1976 4) Josuttis, Nicolai The Standard C++ Library, Addison Wesley, 1999 28