COMS 1003: Introduction to Computer Programming in CFunctionsSeptember 29th 2005Announcements●Use the blog/bboard●Do the readings (brain surgery)●Start your HW if you haven't! 0,2●All HW's will be released shortly●Submission Instructions●HW1 hint: the 'mod' operator (%)Outline●Review●Develop sample line program from scratch●Demonstrate progressive development–ACM article for today–rework line program to use functions●FunctionsFunctions●Group of related statements●Represent a single logical task●Take input, process it, produce outputFunction Syntax●Function declaration–signature●Function definition–signature definition–body (i.e., the work or statements)Function Signature●Name (must be unique!)●Return type (possibly void)●Parameter type list (possibly void)int line(int slope, int x, int xdelta);int line(int, int, int);int show_money(void);int main(int argc, char* argv[]);void print_it(float f);Function Definition●Signature–argument list–variable names●body●defines new scope●static or globally visibleExample Functionfloat average(float sum, int num_students){ float avg = 0.0; avg = (float)(sum/((float)num_students)); return avg;}int and(int p, int q){ return (p&&q);}int add(int a, int b){return (a+b);}Aside: Improving the Functionfloat average(float sum, int num_students){ float avg = 0.0; if(num_students<=0) return 0.0; avg = (float)(sum/((float)num_students)); return avg;}Functions Define Control Flow●A function can “call” other functions●Remember assembly language exampleint main(int argc, char* argv[]){ printf(“hello, world.\n”); return 0;}Call Graphint a(int x){ if(x>5) return b(x); else return c();}int b(int y){ return y+c();}int c(){ return 5;}x<=5a()b()c()x>5Self-Calling Functions●“Recursion”●Entirely legal, but must handle with care//what do I do?int a(int x){ return
View Full Document