imperative programming. heart of program is assignment statements aware that memory contains...
TRANSCRIPT
![Page 1: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/1.jpg)
Imperative Programming
![Page 2: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/2.jpg)
Imperative Programming
• Heart of program is assignment statements
• Aware that memory contains instructions and data values
• Commands: variable declarations, loops, conditionals, procedural abstraction
• Commands make a flowchart
![Page 3: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/3.jpg)
Imperative Languages
• FORTRAN• COBOL• BASIC• Pascal• C
![Page 4: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/4.jpg)
Pascal Example
program Loops; var i: Integer; begin for i := 1 to 10 do begin Writeln('Hello'); Writeln('This is loop ',i); end;end.
![Page 5: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/5.jpg)
C Example
/*Hello, world” */#include <stdio.h>
main(){ printf(“Howdy, earth! \n”); return 0;}
![Page 6: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/6.jpg)
C Printing
• Print strings directly• Other types must be converted, because
they are really numbers:
char c = ‘A’;printf(“Print char c: %c.\n”,c);int n = 6;printf(“Print int n: %d.\n”,n);
![Page 7: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/7.jpg)
Increment/Decrement(P.S. Same in Java)
int x,y,z;x=y=z=1;y = x++;x=1;z = ++x;printf("x++ gives %d\n",y);printf("++x gives %d\n",z);
![Page 8: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/8.jpg)
C-isms
• 0 is false, 1 is true (or non-zero)• Same logical operators, &&, ||, !• But there are also bitwise operators
&, |, ~• Other bitwise operators: ^, >>, <<• x ? y : z means “if x, y, else z”
(Same as Java)char c = x > 0? ‘T’ : ‘F’
![Page 9: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/9.jpg)
Memory Addresses (C)
• Pointers are addresses of where values are stored in memory.
• &variable means “address of variable”• What happens?
int a = 13;printf("a is %d\n",a);printf("&a is %p\n",&a);
![Page 10: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/10.jpg)
Pointers in C
int* ptr; -> ptr is a pointer to an int (with no pointee!)
What does it do?
int *ptr;int num = 4;ptr = #
![Page 11: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/11.jpg)
More Pointer Fun in C
int *ptr;int num = 4;ptr = #*ptr = 5;
![Page 12: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/12.jpg)
Pointers in Pascal
program Pointers; var p: ^integer; begin new(p); p^ := 3; writeln(p^); dispose(p);end.
![Page 13: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/13.jpg)
C Arrays
int arInt[5];int arInt2[5] = {1,4,3,2,1};int ar[2][3] = {{1,2,3},{4,5,6}};
![Page 14: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/14.jpg)
C Strings
char str[] = “I like C.”;char *ptrStr = “I love pointers.”;
Last character in String is ‘\0’
![Page 15: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/15.jpg)
C Strings – Example:Compute length of string
int myStrLen(const char* s){int count = 0;while (*s != ‘\0’){
count++;s++;
}return count;
}
![Page 16: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/16.jpg)
Preprocessor Directives
• #define – substitute before compiling:
#define MAX 1000
if(x > MAX)…
![Page 17: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/17.jpg)
C Scope Modifiers
• static: don’t delete from memory - permanent duration
• register: suggest use of register for variable. Can’t ask for address of the variable
• const: make a variable constant. When pointers are const, their “pointees” are constant
![Page 18: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/18.jpg)
Memory
• malloc: allocates memory. Must give number of bytes wanted. malloc function returns a pointer to the memory. (Null ptr if memory error).
• free: frees memory.• char *ptr;• char str[] = “String!”;• ptr = malloc(strlen(str) + 1);• free(ptr);
![Page 19: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/19.jpg)
Defining new types
• Struct (record in other langs)typedef struct{
char* word;
int frequency;
int size;
} wordInfo;
wordInfo wordOne;
wordOne.word = “apple”;
wordOne.frequency = 0;
wordOne.size = 0;
![Page 20: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/20.jpg)
Pointers to Structs
• We often use pointers to structs (do you know why?
wordInfo *wiPtr;• Can dereference explicitly:
wordInfo wi = *wiPtr;• Or shortcut straight to the fields:
int f = wiPtr->frequency;
![Page 21: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/21.jpg)
What C Lacks
• Iterators• Exception Handling• Overloading• Generics
![Page 22: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/22.jpg)
Parameter Passing
• Pass by value vs Pass by reference• What’s it like in Java?
![Page 23: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/23.jpg)
Parameter Passing Mechanisms
• By value• By reference• By value-result• By name
![Page 24: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/24.jpg)
Pass by Value
• Compute the value of the argument at the time of the call and assign that value to the parameter.
• So passing by value doesn’t normally allow the called function to modify an argument’s value.
• All arguments in C and Java are passed by value.
• But references can be passed to allow argument values to be modified. E.g., void swap(int *a, int *b) { … }
![Page 25: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/25.jpg)
A Look at Swapping - Java 1• void swap(int a, int b){• int t = a;• a = b;• b = a;• }
• int x = 3;• int y = 4;• swap(x,y);• System.out.println("x value: "+x+" y value: "+y);
![Page 26: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/26.jpg)
A Look at Swapping - Java 2
• void swap2(int[] array, int a, int b){• int t = array[a];• array[a] = array[b];• array[b] = t;• }
• int[] arr = {0,1};• swap2(arr, 0,1);• System.out.println("array: "+arr[0]+” "+arr[1]);
![Page 27: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/27.jpg)
A Look at Swapping - Java 3• void swap3(Double a, Double b){• Double t = a;• a = b;• b = t;• }
• Double a = new Double(3.0);• Double b = new Double(4.0);• swap3(a,b);• System.out.println("a value: "+a.doubleValue()+" b value:
"+b.doubleValue());
![Page 28: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/28.jpg)
A Look at Swapping - C1• void swap(int a, int b){• int t = a;• a = b;• b = t;• }• • int main(void){• int a = 3;• int b = 4;• swap(a,b);• printf("a value is %d and b value is %d", a, b);• return 0;• }
![Page 29: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/29.jpg)
A Look at Swapping - C2• void swap2(int* a, int* b){• int temp = *a;• *a = *b;• *b = temp;• }• • int main(void){• int a = 3;• int b = 4;• swap2(&a,&b);• printf("a value is %d and b value is %d", a, b);• return 0;• }
![Page 30: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/30.jpg)
C++ Swapping - Pass by Reference/Value
• int main(void){• int a = 3;• int b = 4;• swap(a,b);• printf("a value is %d and b value is %d", a, b);• return 0;• }
void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
void swap(int& a, int& b){
int temp = a;
a = b;
b = temp;
}
![Page 31: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/31.jpg)
Pass by Reference
Compute the address of the argument at the time of the call and assign it to the parameter.
Example
Since h is passed by reference, its value
changes during the call to B.
int h, i;void B(int* w) { int j, k; i = 2*(*w); *w = *w+1;} void A(int* x, int* y) { bool i, j; B(&h);}int main() { int a, b; h = 5; a = 3; b = 2; A(&a, &b);}
![Page 32: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/32.jpg)
Pass by Value-Result
• Pass by value at the time of the call and copy the result back to the argument at the end of the call. – E.g., Ada’s in out parameter can be implemented
as value-result.– Value-result is often called copy-in-copy-out.
• Reference and value-result are the same, except when aliasing occurs.
![Page 33: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/33.jpg)
Aliasing
• 2 Names for same thing: • Java example:• Object o = new Object…• Object x = o;• Can happen other ways as well:• Pass variable as parameter and refer to it
globally • Pass variable by reference twice
![Page 34: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/34.jpg)
When Pass-by-Value-Result gives different answers…
• (Ada:) • procedure f (x,y: in out Integer) is • begin• x := x + 1;
y := y + 1;• end f;
• f(a,a) increments a by ONE only.
![Page 35: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/35.jpg)
More on Pass by Value-Result• -Sometimes the order of copying makes a
difference! Different with different implementations of language.
• Ex:• f(i,a[i]) -> change i first or a[i] first?• -All actual parameters must be l-values - things
that can be assigned. (Same in pass-by-reference!)
• Ex: • f(a+1) -> How would it be copied back in?
![Page 36: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/36.jpg)
Pass by Name
• Textually substitute the argument for every instance of its corresponding parameter in the function body.
• – Originated with Algol 60 (Jensen’s device), but
was dropped by Algol’s successors -- Pascal, Ada, Modula.
– Exemplifies late binding, since evaluation of the argument is delayed until its occurrence in the function body is actually executed.
– Associated with lazy evaluation in functional languages
![Page 37: Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,](https://reader036.vdocument.in/reader036/viewer/2022062407/56649dd15503460f94ac7a6d/html5/thumbnails/37.jpg)
When Pass by Name Gives different results
• Algol 60:• procedure
swap(a,b);• integer a,b;• begin integer t;• t := a;• a := b;• b:= t• end;
Call swap(i, a[i]). Method becomes:
begin integer t;
t := i;
i := a[i];
a[i]:= t
end;
i = 0, a=[3,0,0,0].
Want: i=3, a = [0,0,0,0].
What really happens?
What happens if I call swap(a[i],i) instead?