CS110_03a_Scope_RelnOps

CS110_03a_Scope_RelnOps - EECS110: 3a Scope, Library...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 3a Scope, Library Functions, & Decision-Making Jack Tumblin jet@cs.northwestern.edu . void is the `empty' Keyword float square(float x); // fcn. prototype Function Declaration & Definition MUST have a returned data type, a function name, a formal parameter list. Some functions don't need any arguments. Some functions don't need to return any data. THEN use void as the argument list, and/or use void as the returned data type: void ejectPage(void); // fcn. prototype Useful Jargon: Scope Identifier == a name used in your program: for a function, variable, directive, library ... Scope of an identifier == all the parts & places of a program where that identifier is valid (Hint; think `telescope' how far away can your identifier see?) EXAMPLES: Scope of a global variable? To the edge of the sky; the whole program from end-to-end, everywhere. Scope of a local variable? Only inside the "black box" that holds it; only in the block of statements or function body where it was declared. declared Example: local variables #include<stdio.h> float square(float x); int main (void) { float in,out; // fcn. prototype Same Variable names?!? --its OK; they're local only in = -4.3; out = square(in); printf("%f squared is %f\n",in,out); return 0; } float square(float x) // fcn. definition { float out; out = x*x; return (out); } #include<stdio.h> float square(float x); int main (void) { float in,out; // fcn. prototype Scope of in,out in = -4.3; out = square(in); printf("%f squared is %f\n",in,out); return 0; } float square(float x) // fcn. definition { float out; Scope of out out = x*x; return (out); } `out' and `out' Stored in two different memory locations, too! Scope of local variables GLOBAL variables: A BAD IDEA! #include<stdio.h> float square(float x); float in; int main (void) { float out; /* fcn. prototype */ /*!BAD! global var!*/ Scope of in in = -4.3; out = square(); printf("%f squared is %f\n",in,out); return 0; } float square(void) /*fcn. definition */ { !DON'T float out; out = in*in; return (out); } DO THIS! GLOBAL VARs prevent truly separate functions! GLOBAL variables: A BAD IDEA! #include<stdio.h> float square(float x); float in; int main (void) { float out; /* fcn. prototype */ /*!BAD! global var!*/ Same Variable names: Same Global Variable in = -4.3; out = square(); printf("%f squared is %f\n",in,out); return 0; } float square(void) /*fcn. definition */ { !DON'T float out; out = in*in; return (out); } DO THIS! GLOBAL VARs prevent truly separate functions! Local Variable Subtleties Declare variables at the beginning of a block CAREFUL! the scope is limited to that block! #include<stdio.h> int main (void) { int i; // for loop: explained soon! for (i=1; i<5; i++) { int j; j=i-1; printf(" %d",j); } j=20; printf("i=%d, j=%d",i,j); } What will happen? what should we do? Local Variable Subtleties Declare variables at the beginning of a block CAREFUL! the scope is limited to that block! #include<stdio.h> int main (void) { int i; What will happen? // for loop: explained soon! what should we do? for (i=1; i<5; i++) { scope of j int j; j=i-1; printf(" %d",j); Error! j is out-of-scope; } it is undefined here. j=20; printf("i=%d, j=%d",i,j); } Local Variable Subtleties Declare variables at the beginning of a block CAREFUL! the scope is limited to that block! #include<stdio.h> int main (void) { int i,j; scope of i,j // for loop: explained soon! for (i=1; i<5; i++) { j=i-1; printf(" %d",j); } j=20; printf("i=%d, j=%d",i,j); } BETTER: Declare all variables At top of block that sets their scope Scope of Global + Local Variables #include<stdio.h> int i; void func(void); scope of global i int main(void) { i = 5; /* set global var */ printf("Before func, i=%d\n", i); func(); printf("After func, i=%d\n", i); } void func() { int i; /* locally i=10; /* ignores printf("Within func, } scope of local i GLOBAL DANGER Reduced by just a bit because... The local definition of i hides the global definition. (AVOID! TRICKY!) Values of `i' Before func, iglobal=5 Within func, ilocal=10 After func, iglobal=5 defined i */ global i */ i=%d\n", i); Libraries: Collections of Functions Your functions have two parts, in two places: declaration ("function prototype") defines function name, inputs and output placed above main() definition ("function body") holds all the statements that do the function's work placed below main. If you write 10,000 functions? Gather them into libraries... C's Ready-Made Functions Many, many useful functions are already written and debugged for you. To use them: #include <something.h> Where are they? varies with compiler used in Visual C++ (default installation) C:/Program Files/Microsoft Visual Studio/VC98/Include (Warning! cluttered with many Windows-only files) In CodeBlocks: C:\...\gcc\include What are they? ... open these files, look at them! C's Ready-Made Functions What are they? (see book, page 1059-1069) Opinion: Most Useful Libraries: stdio.h (printf, scanf, files, other input/output) stdlib.h math.h minmax.h time.h string.h ctype.h conio.h errno.h (almost everything! need more? (transcendentals: sin,cos, tan, exp, pow...) (min(a,b), max(a,b)) (clock and timing functions) (search, match, rearrange text) (Chars: toupper,tolower, isalpha, isdigit,...) (kbhit,...) (main() return error codes) . . . (see book, pg. 1059-1069 for more)... Preliminaries: What Decisions Can A Machine Make? Only those we can write as an orderly set of rules, built up from these operators inside expressions Relational: "given these 2 numbers, tell us true/false" (is A > B? is A exactly equal to B?) Logical: "Given 2 true/false, tell us true/false" (Is A the opposite of B? is (A AND B) true? ) Relational Operators Operator < > <= >= Meaning Less than Greater than Less than or equal Greater than or equal Is Equal Is Not Equal == != Result is always either TRUE or FALSE Relational Operators Example: The value of the expression (3 < 2) is FALSE The value of the expression (2 == 2) is TRUE In C (and many other languages), TRUE is represented by an (int) 1 FALSE is represented by an (int) 0 All nonzero numbers (int) are TRUE Relational vs. Arithmetic Ops: BOTH use numerical inputs: Example: (num < 10) and (num + 10) (where num is an integer variable ) But relational outputs are only TRUE / FALSE. (number < 10) produces a TRUE or FALSE (0/1) (number + 10) produces any number Logical Operators Different from Relational Operators because: Inputs: TRUE or FALSE Outputs: TRUE or FALSE Operator && || ! Action AND OR NOT Logical Operators What do they mean? Look at Truth Table: p T T F F q T F T F p&&q (and) T F F F p||q (or) T T T F !p (not) F F T T Logical Operators Examples: int x,y; x=10; y=-8; Now evaluate these expressions: (x+5) < (12-y) (x>5) || (y>10) Logical Operators Examples: int x,y; x=10; y=-8; Now evaluate these expressions: (x+5) < (12-y) (10+5) < (12- (-8)) 15< 20 TRUE (x>5) || (y>10) (10>5) || (-8>10) (TRUE) || (FALSE) TRUE Recall: Assignment Operator = In C, = is the assignment operator, it is NOT the same as an `equals' sign. It COPIES the value on the right into the variable on the left. (remember, = ) The left side MUST be single variable (an `l-value') The right side MUST be either a value or an expression NEW! `IsEqual' operator == It is NOT the same as an `equals' sign; it is a relational operator. It compares the left-side operand (or `term') to the right-side operand (or `term') It returns TRUE or FALSE TRUE only if values on both sides match FALSE if values do not match Operator Precedence: ! Arithmetic, Relational, Logical, Assignment Unary - * / % + < <= >= > = = != && || = Higher precedence (do these first) first Associativity: execute left-to-right (except for = and unary (see book)) Example: 5+2-4+3 7-4+3 3+3 6 ...
View Full Document

Ask a homework question - tutors are online