Programming Languages and Paradigms Imperative Programming 6152005 Imperative

  • Slides: 40
Download presentation
Programming Languages and Paradigms Imperative Programming 6/15/2005

Programming Languages and Paradigms Imperative Programming 6/15/2005

Imperative Programming n n Variables, assignment, sequencing, iteration, procedures as units State-based, assignment-oriented Global

Imperative Programming n n 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. 2

Data and Computation n n Binding Data n n n Variables Data types Computation

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

Binding n Program units/entities have attributes n n Binding n n e. g. ,

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

Binding Time n Language definition time n n Language implementation time n n example:

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

Variable n n A named location in memory that can hold a value Formally,

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

Name and Scope n n Declaration Identifier rules and significant characters Name bound during

Name and Scope n n Declaration Identifier rules and significant characters Name bound during compile time Scope n n 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. 7

Type n Consists of n n n Built-in/Primitive vs User-defined types n n binding?

Type n Consists of n n n Built-in/Primitive vs User-defined types n n binding? Implicit declarations n n Set of values Operations 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. 8

Why Use Data Types? n Purpose: classification and protection n n note that all

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

Complex Data Types n n n User-defined enumeration types Composite types Aggregations n n

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

L-value and r-value n l-value: address/location n lifetime memory allocation r-value: contents/encoded value n

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

Pointers n n n Attributes Allocation and de-allocation Operators n n n referencing (address-of)

Pointers n n n Attributes Allocation and de-allocation Operators n n n 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. 12

Control: Statements and Routines n n Expressions and statements Conditional execution Iteration Routines n

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

Expressions and Statements n n Operands: variables, literals Operators n n Unary, binary, and

Expressions and Statements n n Operands: variables, literals Operators n n Unary, binary, and others (functions? ) Value returned vs effect Precedence Statements n n n 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. 14

Conditional Execution n n Conditions and boolean values Relationship of conditions and int in

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

Iteration n For statement n n n Loop control variable The while loop while

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

Routines n n Program unit; sequence of instructions Also a 5 -tuple: n n

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

Evolution: statements & goto, then structure, then routines n First there was machine language,

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

Evolution: statements & goto, then structure, then routines Copyright 2006, by the authors of

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

About Routines n n Functions versus procedures Parameter Passing n n n By value

About Routines n n Functions versus procedures Parameter Passing n n n 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. 20

Functions versus Procedures n n n Difference: functions return a value, procedures don’t Languages

Functions versus Procedures n n n 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. 21

Parameters n n n Formal parameters: indicated in routine definition Actual parameters: indicated in

Parameters n n n Formal parameters: indicated in routine definition Actual parameters: indicated in invocation Parameter passing methods n n 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. 22

Call by Value n n Formal parameter is a local variable Actual parameter serves

Call by Value n n 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: n n 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. 23

Call by Reference n Formal parameter serves as an alias/synonym for the actual parameter

Call by Reference n Formal parameter serves as an alias/synonym for the actual parameter during the invocation n Formal and actual parameters share the same l-value Actual parameter must be a variable (or have an l -value) Language examples n n 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. 24

Call by Value Result n n Copy-in/copy-out Actual parameters are initially copied into the

Call by Value Result n n 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 n 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. 25

Call by Reference versus Call by Value-Result int a = 5; change( a );

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; x++; } Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 26

Macro Substitution n n In C, #define directive can contain parameters Example: #define sqr(x)

Macro Substitution n n 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. 27

Activation Records n n Activation record (or frame): contains the data needed for the

Activation Records n n Activation record (or frame): contains the data needed for the activation of a routine Typical Contents n n n 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. 28

Activation Records in C Copyright 2006, by the authors of these slides, and Ateneo

Activation Records in C Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 29

Activation Records in Pascal Copyright 2006, by the authors of these slides, and Ateneo

Activation Records in Pascal Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 30

Nested Calls n Suppose the functions main and f are defined as follows: n

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

Recursion n Activation records using runtime stacks support recursion Recursive calls create separate activations

Recursion n Activation records using runtime stacks support recursion Recursive calls create separate activations of the function Example: binary search Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 32

Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All

Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 33

Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All

Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 34

Links in Activation Records n Control links provide access to calling frames n n

Links in Activation Records n Control links provide access to calling frames n n n Used to restore to calling frame when the called routine completes Access links provide access to non-local data In the next example, take note of the data that the procedures need access to Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 35

Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All

Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 36

Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All

Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. 37

Modules and Program Structure n Programming in the Large n n Modules n need

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

Modularity Considerations n n Interface and Implementation Compilation n Independent Separate Libraries Copyright 2006,

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

Summary n Imperative Programming is about n n Data (variables) and statements affecting that

Summary n Imperative Programming is about n n 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. 40