Lecture 6 -------------- Announcements ----------------- - Homework 3 is due - Homework 4 is posted, due Oct 6th at 1 - Project 1 is posted, due Oct 10th at midnight - start early, send email questions - although i'm online a lot, do not depend on it! Today Outline ----------------- - review of conditionals/loops - statment blocks/nesting - variable scope - C functions Found Out ----------- #define FOO Review of Conditionals/Loops ----------------------- - if or if/else for single branch - if else if for multiple branches and multiple expressions - switch case,look in book ch 3 - loops all equiv - for, while - do while do while(); Expressions for all we've seen --------------------------------- - every expression in C results in some sort of number, which can be used in a conditional! - function calls, assignments, binary ops ex: if ( !scanf("%d", &myint) ) { printf("uh oh didn't read an int\n"); } else { printf("all is well\n"); } Statements for all we've seen ----------------------------- - statements are lines that end in a ; - ops, function calls, variable declarations - anywhere where you can put statement, can put block - surrounded by {} - if () { stat1; stat2; ... } for(.....) { stat1; stat2; } - even put them in the middle of nowhere int a; a=10; { printf("%d\n", a); } - why? encapsulation of statements for functions and control structs - variable scope, regions where variable is accessible ** - outer variables accessible by inner nests, not vice versa - variable is removed when scope is left - variables declared at beginning of stat blocks (ISO C89) main() { int a; { int b; // can use a and b here } // can only use a } - variables can be overridden main() { int a; { int a; int b; // local version of a and b } // back to normal a } - since variable visibility goes from outer nests to inner, - what happens when i declare a variable outside of main? C functions --------------- - so far we're able to write monolithic programs, one function - larger programs may get extremely complex, hard to read - also, we may have to repeat entire chunks of code with only slight variable differences ex: float a,b,c,d; char cfi, cli, dfi, dli; ... get initials for person c and d printf("%c.%c., enter body temp: ", cfi, cli); scanf("%f", &c); a = 5*(c-32)/9; printf("body temp in farh is %f\n", a); ...do something else printf("%c.%c., enter body temp: ", dfi, dli); scanf("%f", &d); b = 5*(d-32)/9; printf("body temp in farh is %f\n", b); ... so some more stuff - we had to write that chunk of code more than once, imagine if we had 2000 temps at random points in the program! - functionality can get even more complex - functions provide a way to encapsulate pieces of code that can be reused - functions allow programmers to share code - form of function ** - prototype - defines function before use so compiler can detect type problems - must be outside other functions - function itself - defined anywhere outside other function (, , ...); (, , , ...) { ... return (); } - function is called from another function - optionally takes parameter values from the calling function - function optionally returns a value to the calling function - function with no parameters and no return value: void foo(void); void foo() { } - function with parameters and return value: - ex: gettemp (call it gettemp) - what is the return type? float - what parameters does it require? person's initials - statments? ** float gettemp(char, char); float gettemp(char fi, char li) { float result; float intemp; printf("%c.%c., enter your body temp: "); scanf("%f", &intemp); result = 5*(intemp - 32)/9; return(result); } - return must be same type as in function def or will be converted (with warning) - full example: main() { char cfi, cli, dfi, dli; float a, b; .. get initals a = gettemp(cfi, cli); ... do stuff b = gettemp(dfi, dli); ... do more stuff } Function Parameters ----------------------------- - when a function with parameters is called, values passed to the functions are COPIED to new variables in the scope of the function itself. - called 'pass by value' - types should match, else they are converted as if they were assigned with = - basic types can be passed in, so can arrays - remember scope, variables inside function are only accessible inside the function ex: main() { int a; a = 10; somefunc(a); printf("%d\n", a); } void somefunc(int a) { a = 20; printf("%d\n", a); } - array ex: main() { int foo[3]; int bar[3][3]; somefunc(foo); otherfunc(bar); } void somefunc(int inarray[]) { } void otherfunc(int inarray[][3]) { }