Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages and Paradigms

Similar presentations


Presentation on theme: "Programming Languages and Paradigms"— Presentation transcript:

1 Programming Languages and Paradigms
Imperative Programming

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

3 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.

4 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.

5 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.

6 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.

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

8 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.

9 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.

10 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.

11 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.

12 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.

13 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.

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

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

16 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.

17 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.

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

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

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

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

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

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

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

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

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

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

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

29 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.

30 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.

31 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.

32 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.

33 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.

34 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.


Download ppt "Programming Languages and Paradigms"

Similar presentations


Ads by Google