Download presentation
Presentation is loading. Please wait.
Published byBrandon Logan Modified over 8 years ago
1
Programming Languages and Paradigms Imperative Programming
2
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 2 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)
3
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 3 Data and Computation Binding Data Variables Data types Computation Assignments and expressions Control structures Subprograms / routines / parameters / activations
4
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 4 Binding Program units/entities have attributes e.g., a variable has a name, a statement has associated actions Binding setting the value of an attribute Binding time when binding occurs
5
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 5 Binding Time Language definition 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
6
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 6 Variable A named location in memory that can hold a value Formally, a 5-tuple: name scope type l-value r-value
7
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 7 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
8
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 8 Type Consists of 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
9
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 9 Why Use Data Types? Purpose: classification and protection note that all data are ultimately represented as bit-strings Advantages: abstraction compile-time checking and resolution explicit specification
10
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 10 Complex Data Types User-defined enumeration types Composite types Aggregations cartesian product (records or structures) mapping (arrays) unions
11
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 11 L-value and r-value l-value: address/location lifetime memory allocation r-value: contents/encoded value initialization constants * binding?
12
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 12 Pointers Attributes Allocation and de-allocation Operators referencing (address-of) de-referencing Synonyms and unnamed variables
13
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 13 Control: Statements and Routines Expressions and statements Conditional execution Iteration Routines Parameter Passing Modules and Program Structure
14
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 14 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
15
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 15 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
16
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 16 Iteration For statement Loop control variable The while loop while vs do-while do-while versus repeat-until Iteration using goto
17
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 17 Routines Program unit; sequence of instructions Also a 5-tuple: name scope type l-value r-value
18
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 18 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
19
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 19 Evolution: statements & goto, then structure, then routines
20
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 20 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
21
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 21 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
22
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 22 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
23
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 23 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
24
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 24 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)
25
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 25 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)
26
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 26 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++; }
27
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 27 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)
28
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 28 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
29
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 29 Activation Records in C
30
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 30 Activation Records in Pascal
31
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 31 Nested Calls 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
32
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 32 Recursion Activation records using runtime stacks support recursion Recursive calls create separate activations of the function Example: binary search
33
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 33
34
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 34
35
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 35 Links in Activation Records Control links provide access to calling frames 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
36
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 36
37
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 37
38
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 38 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
39
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 39 Modularity Considerations Interface and Implementation Compilation Independent Separate Libraries
40
Copyright 2006, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L3: Imperative Prog Slide 40 Summary Imperative Programming is about 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...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.