Rehab Ben Abdulla [email protected] Imperative programming paradigms 1 Rehab ben Abdulla
Imperative programming paradigms Imperative programming focuses on describing how a program operates. Imperative programs describe the details of HOW the results are to be obtained, in terms of the underlying machine model. An imperative program consists of commands for the computer to perform. In imperative paradigm we solve problems step by step by telling the computer what to do next. 2 Rehab ben Abdulla
Imperative programming paradigms An imperative program is a list, or sequence, of instructions. The execution of each operation can alter the memory state. Imperative programming allows side effects. Change the value of variables. This style of programming is directly inspired by assembly programming. You find it in the earliest general-purpose programming languages (Fortran, C, Pascal, etc.) 3 Rehab ben Abdulla
Imperative programming paradigms Data and Computation In imperative programming we must consider how the data is represented and how the computations are carried out. Data Variables Data types Computation Assignments and expressions Control structures Subprograms / routines 4 Rehab ben Abdulla
Data and Computation Name and Scope Declaration Identifier rules and significant characters Scope range of instructions over which variable name is known namespaces Blocks (as in Pascal or C) Static vs dynamic scope binding 5 Rehab ben Abdulla
Data and Computation 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 6 Rehab ben Abdulla
Data and Computation 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 Foxpro Dynamic typing 7 Rehab ben Abdulla
Data and Computation Complex Data Types User-defined enumeration types Composite types Aggregations cartesian product (records or structures) mapping (arrays) Unions Abstract Data Types. ADTs specify/describe behaviors. classes 8 Rehab ben Abdulla
Data and Computation Variable A named location in memory that can hold a value name scope type l-value r-value 9 Rehab ben Abdulla
Data and Computation 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 Language definition time Language implementation time Compile-time Execution-time 10 Rehab ben Abdulla
Data and Computation Control Statements and Routines Expressions and statements Conditional execution Iteration Routines Parameter Passing Modules and Program Structure 11 Rehab ben Abdulla
Data and Computation Routines Program unit; sequence of instructions Also a 5-tuple: name scope type l-value r-value Parameter Passing By value By reference Others? Activation Records Recursion 12 Rehab ben Abdulla
Procedural programming paradigms Procedural programming can be defined as a subtype of imperative programming as a programming paradigm. Based upon the concept of procedure calls. Statements are structured into procedures (also known as subroutines or functions). Why we need procedures or functions? 13 Rehab ben Abdulla
Procedural programming paradigms The focus of procedural programming is to break down a programming task into a collection of variables, data structures, and subroutines. Writing good functions and procedures use the most appropriate implementation and employ correct efficient algorithms To get the benefit of: Code reuse. Modifiable or updateable variables (side effect). Abstraction and modularity. Hiding details 14 Rehab ben Abdulla
Case study: stack Stack’s rule is: FILO (first in last out). The tasks are: Push and pop operations. The data can be implemented as: Array, linked list or other implementations. 15 Rehab ben Abdulla
Stack program in C serial program Void main() { Int max=100; Int MyStack [max]; Int top=0; Int x=7; if(top<max) MyStack [top++]=x; elas printf (“full”) if(top>0) x=( MyStack [top--]); elas printf (“empty ”) ... ... } 16 Rehab ben Abdulla
Stack program in C structured program Int max=100; Int MyStack [max]; Int top=0; Void push( int x) { if(top<max) MyStack [top++]=x; else printf (“full”); } int pop() { if(top>0) return( MyStack [top--]); elas printf (“empty”) } Void main() { Push(2); Push(5); Push(6); Printf (“% d”,Pop ()); ... } 17 Rehab ben Abdulla
Stack program in C One source file MyStack and top are global variables Stack and application functions defined at the same level (file) 18 Rehab ben Abdulla
Procedural programming paradigms: Emphasis on procedural abstraction. Top-down design; Step-wise refinement. Suited for programming in the small. New operations cause additive changes in procedural style, New data representations require modifications to all “procedure modules”. 19 Rehab ben Abdulla