CS 228 meeting -*- Outline -*- * control abstraction (HR 1) every algorithm has both data and control flow. data is information control flow means order in which instructions exectued ** goals of chapter 1 discuss abstraction and its importance using control flow discuss functions in C++, also top-down design, and testing methods discuss program style and documentation conventions ** control structures point is that control structures are abstarctions of lower-level stuff ------------------------- CONTROL STRUCTURES ARE ABSTRACTIONS 160 IF I >= J GO TO 163 161 LET I = I + 3 162 GO TO 160 163 LET K = 5 while (I < J) { I = I + 3; } K = 5; 170 LET X = I 171 LET Y = J 172 GO SUB 500 173 LET Z = X Z = max(I, J); ------------------------- in machines and in imperative languages, control flows from one instruction to the next, in machines (and in the BASIC code shown) flow can be altered by conditional tests and goto stamements in C++ have control structures like while do do this Subroutines (functions in C++) are also control abstractions *** gotos also in C++ main purpose (now) is early exit from loops. Can do some kinds of goto without it, by using return, break, continue or the standard function exit *** when to use what use for when need definite iteration (counting, arrays) use while or do-while otherwise, use do-while only when need at least one execution ** functional abstraction ------------------------ FUNCTIONAL ABSTRACTION function specification separates: behavior (what it does) from implementation (how it does it) double sqrt(double x) // PRE: x >= 0.0 // POST: FCTVAL is approximately the // positive square root of x ------------------------ this is also called procedural abstraction we call it functional because C++ subroutines are "functions" motto: no abstraction without specification Note that the spec doesn't say how the square root is computed (e.g., by Newton's method). may also need description of performance, information accessible from the implementation, but also needed... *** side effects (functions vs. procedures) ------------------------- SIDE EFFECTS def: a *side effect* of a function is any change to an object (variable) made by the function ---------------------------- this includes I/O, but not changes made to variables local to the function Q: what operators in C++ have side-effects? list them this implies the "normal" thing is to just return a value Some common terminology (as in Pascal): def: a *procedure* has no return value only side effects (C++ void function, sometimes called pure procedures) def: a *function* has no side effects, only a return value mixes also possible *** parameter passage ------------------------- PARAMETER PASSAGE parameter values may flow: NAME EXPLANATION C++ FORMAL in in to a function int in out out of a function int &out inout in and out int &inout -------------------------- Headington and Riley annotate code with C-style comments for these, we'll use the modifies clause **** parameter modes --------------------------- in and out parameters void assign2(int& v1, int& v2, int e1, int e2) // MODIFIES: // POST: { v1 = e1; v2 = e2; } -------------------------- write out the MODIFIES and post clauses -------------------------- example of use: int a = 4; int b = 5; assign2(a, b, b + 3, a); ------------------------- draw pictures to show what happens might want to mention that this can be inlined by writing inline void assign2(...) (if time) show how to use this in doing fibonacci ---------------------- inout parameters #include "assign2.h" void swap(int& x, int& y) // MODIFIES: // POST: { assign2(x, y, y, x); } ----------------------- write out the MODIFIES and post clauses ----------------------- example of use: int c = 28; int d = 200; swap(d, c); ----------------------- **** passing arrays Simple but not quite accurate story: ------------------------------ ARRAY PARAMETERS Arrays are passed by reference void initialize(int a[], int size) // PRE: size >= 0 // MODIFIES: // POST: { for (int i = 0; i < size; i++) { a[i] = 0; } } ------------------------------ draw picture you can't pass by value, if want to, can prohibit changing elements by use of const -------------------------------- CONST PARAMETERS int array_sum(const int a[], int size) // PRE: size >= 0 // MODIFIES: // POST: FCTVAL is the sum of the // elements a[0] ... a[size-1]. { int total = 0; for (int i = 0; i < size; i++) { total += a[i]; } } -------------------------------- **** exercise ------------------------------- FOR YOU TO DO fill in the comments below: void sq_each(int arr[], int maxi) // PRE: // MODIFIES: // POST: { for (int i = 0; i < maxi; i += 1) { arr[i] *= arr[i]; } } then answer: what is the mode of each parameter? (in, out, inout?) -------------------------------