Programming Languages and Paradigms

Slides:



Advertisements
Similar presentations
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Advertisements

Programming Languages and Paradigms The C Programming Language.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Names and Bindings.
Subprograms The basic abstraction mechanism. Functions correspond to the mathematical notion of computation input output procedures affect the environment,
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 10 Implementing Subprograms.
Runtime Environments Source language issues Storage organization
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Run time vs. Compile time
The Concept of Variables
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Chapter 9: Subprogram Control
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Imperative Programming
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Basic Semantics Associating meaning with language entities.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
ISBN Chapter 10 Implementing Subprograms.
Names, Scope, and Bindings Programming Languages and Paradigms.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
ISBN Chapter 10 Implementing Subprograms.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Implementing Subprograms
Definition of the Programming Language CPRL
Data Types In Text: Chapter 6.
Chapter 4 Variables & Binding
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Programming Languages and Paradigms
Subprograms The basic abstraction mechanism.
Programming Paradigms
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Chap. 6 :: Control Flow Michael L. Scott.
Implementing Subprograms
Implementing Subprograms
Final Review In Text: Chapters 1-3, 5-11,
Abstract Data Types and Encapsulation Concepts
Programming Languages and Paradigms
Midterm Review In Text: Chapters 1-3, 5-10, 15.
Chap. 6 :: Control Flow Michael L. Scott.
Final Review In Text: Chapters 1-3, 5-12,
Final Review In Text: Chapters 1-3, 5-16.
UNIT V Run Time Environments.
Final Review In Text: Chapters 1-3, 5-16.
Names and Binding In Text: Chapter 5.
Overview of Programming Paradigms
Programming Languages and Paradigms
Final Review In Text: Chapters 1-3, 5-16.
Programming Languages and Paradigms
Midterm Review In Text: Chapters 1-3, 5-11, 15.
Implementing Subprograms
Implementing Subprograms
Presentation transcript:

Programming Languages and Paradigms Imperative Programming

Imperative Programming Variables, assignment, sequencing, iteration, procedures as units State-based, assignment-oriented Global variables, side effects Program units: Data (Variables) and Computation (Statements and Routines) Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Data and Computation Binding Data Computation Variables Data types Assignments and expressions Control structures Subprograms / routines Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Binding Program units/entities have attributes Binding Binding time e.g., a variable has a name, a statement has associated actions Binding setting the value of an attribute Binding time when binding occurs Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Binding Time Language definition time Language implementation time example: size of an int in Java Language implementation time example: size of an int in C Compile-time example: type of a particular variable in C/Java Execution-time example: memory address of a variable Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Variable A named location in memory that can hold a value Formally, a 5-tuple: name scope type l-value r-value Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Name and Scope Declaration Identifier rules and significant characters Name bound during compile time Scope range of instructions over which variable name is known namespaces Blocks (as in Pascal or C) Static vs dynamic scope binding Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Type Consists of Built-in/Primitive vs User-defined types Set of values Operations Built-in/Primitive vs User-defined types binding? Implicit declarations e.g., FORTRAN and first letter of a variable and first assignment in BASIC or Perl or PHP Dynamic typing Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Why Use Data Types? Purpose: classification and protection Advantages: note that all data are ultimately represented as bit-strings Advantages: abstraction compile-time checking and resolution explicit specification Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Complex Data Types User-defined enumeration types Composite types Aggregations cartesian product (records or structures) mapping (arrays) unions Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L-value and r-value l-value: address/location lifetime memory allocation r-value: contents/encoded value initialization constants * binding? Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Pointers Attributes Allocation and de-allocation Operators referencing (address-of) de-referencing Synonyms and unnamed variables Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Control: Statements and Routines Expressions and statements Conditional execution Iteration Routines Parameter Passing Modules and Program Structure Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Expressions and Statements Operands: variables, literals Operators Unary, binary, and others (functions?) Value returned vs effect Precedence Statements The semicolon: C (terminator) vs Pascal (separator) Blocks: C { } vs Pascal (begin-end) Control structures: decision, iteration, routines Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Conditional Execution Conditions and boolean values Relationship of conditions and int in C Not adopted in Java Short-circuiting If-statements and dangling else The switch statement Restrictions The break; statement Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Iteration For statement The while loop while vs do-while Loop control variable The while loop while vs do-while do-while versus repeat-until Iteration using goto Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Routines Program unit; sequence of instructions Also a 5-tuple: name scope type l-value r-value Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Evolution: statements & goto, then structure, then routines First there was machine language, then came Fortran: Improved statement readability The goto statement Then structured programming: Blocks Control structures (e.g., while-do) And routines/procedures: Scope, activation, recursion Parameter passing Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Evolution: statements & goto, then structure, then routines Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

About Routines Functions versus procedures Parameter Passing By value By reference Others? (call by value-result, call by name) Procedures as parameters Activation Records Recursion Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Functions versus Procedures Difference: functions return a value, procedures don’t Languages treat this distinction differently C: procedures are just functions that don’t return anything (void) Modula: functions are just procedures with a return type Pascal: some restrictions on function parameters Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Parameters Formal parameters: indicated in routine definition Actual parameters: indicated in invocation Parameter passing methods Call by value Call by reference Call by value-result (copy-in, copy-out) Others: Call by name/macro substitution Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Call by Value Formal parameter is a local variable Actual parameter serves as an initial value for the formal parameter Actual parameter could be an expression Language examples: In C, all parameters are called by value, use of pointers is just a workaround In Pascal, call by value is the default, but call by reference is supported Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Call by Reference Formal parameter serves as an alias/synonym for the actual parameter during the invocation Formal and actual parameters share the same l-value Actual parameter must be a variable (or have an l-value) Language examples Pascal (precede formal parameter declaration with var; also called pass by variable) C++ (append an & to the variable type in formal parameter) Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Call by Value Result Copy-in/copy-out Actual parameters are initially copied into the formal parameters Formal parameter values are copied back to actual parameters after routine completes execution In and out parameters are sometimes distinguished Language example: ADA (in, out, and in-out parameters) Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Call by Reference versus Call by Value-Result int a = 5; change( a ); // value of a? By Reference: a = 11 By Value-Result: a = 7 void change( int x ) { x++; a = 10; } Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Macro Substitution In C, #define directive can contain parameters Example: #define sqr(x) x*x The statement ans = sqr(3*a); is expanded to become ans = 3*a*3*a; Careful because sqr(3+1) expands to 3+1*3+1 Better version: #define sqr(x) (x)*(x) Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Activation Records Activation record (or frame): contains the data needed for the activation of a routine Typical Contents Local variables Formal parameters Function result, if applicable Control link (to activation record of caller) and access link (to access other data within scope) Activation records stored in a runtime stack Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Illustration Suppose the functions main and f are defined as follows: void main() { f(); g(); } void f() { g(); } There are two different activations of g() when the program executes g f main g main Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Recursion Activation records using runtime stacks support recursion Recursive calls create separate activations of the function factorial … main Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

More About Activations Control and access links serve to provide a method to get to calling frames and non-local data More examples are available in a separate set of slides Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Modules and Program Structure Programming in the Large need to compose program through units Modules program units that interact with each another modules in turn contain their own data and routines Encapsulation information hiding independent modules with interfaces Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Modularity Considerations Interface and Implementation Compilation Independent Separate Libraries Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Summary Imperative Programming is about On to the next paradigm . . . Data (variables) and statements affecting that data Control-flow constructs enrich statement specification Routines and modules help impose program organization On to the next paradigm . . . Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved.