lecture11-feb12

# lecture11-feb12 - Announcements Lecture 11 Read Goto...

This preview shows pages 1–3. Sign up to view the full content.

1 Announcements - Lecture 11 • Read “Goto” Considered Harmful goto statements are no longer allowed – All programs can be implemented with only sequence, selection and iteration logic constructs – Example in the book on pg. 114 is wrong! • Assignment 3 out on Monday – Coding standards are in effect • Exam 1 a week from today in class (Ch. 1-9) – Sample exam to be posted on Tuesday • Topics for today – Functions (Ch. 9) and functional programming Book Example – pg. 114 • Problem – you need to break out of a loop from within a switch construct or a nested loop /* with goto */ while ( . . . ) { switch (expression) { case 1: … . . .goto loopDone; } } loopDone: . . . /* without goto */ int fag = FALSE; { switch (expression) { case 1: … . . {fag = TRUE; break;} } } . . . Functions in a Nutshell • A function is a series of statements called by their given name (e.g. main ) • It is the essential building block for complex C programs – It is how we divide our large program into smaller sub- programs to manage complexity • Behavior is similar to a mathematical function: y = f ( x,z ) Arguments are values passed in ( ) – optional – Returns a value through the function name – optional • Many previously written standard functions are in the library for your use You can write your own functions too, and then call them or reuse them A Function as a Module values sent in from function calling statement via the arguments supplied in ( ) (optional) Resultant value returned (optional), or function actions that have some other effect Function operation As in, for example: double x, y, z; /* assume they get values */ y = sqrt (x); getchar ( ); printf (“hello\n”); z = average (x, y); double average (double a, double b) { return (a + b) / 2.0; } C Standard Library Functions • See appendix D of the book for all the functions available • In various library (header) files (.h) – stdlib – stdio – math – time – ctype – string – assert a few others Top Down Functional Programming main sub1 sub2 sub3 sub4 subsub1 subsub2 subsub3 subsub4 sss1 sss4 calls calls calls calls A ±unctional hierarchy diagram

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 How does this work? • Partitioning the problem – Let’s us divide a big problem into small problems – No one part is highly complex – However a balance must be achieved between too many and too few functions • Too few may lead to complexity of the size of each • Too many may lead to complexity of the number of interconnects • Many ways to partition a problem – this is one of the fundamentals of the craft of design • Identifying relatively independent functions is key to – Reducing complexity – Increasing clarity – Increasing reusability – Modularizing our programs • A function operates on data passed through the
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

lecture11-feb12 - Announcements Lecture 11 Read Goto...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online