CSC161 2010F, Class 21: Functions and Parameters Overview: * Basics of functions in C. * Questions and answers. * Lab! Admin: * Candy * CS Table today: Is Google Evil? And other tales of the Web. * There is no assignment for Wednesday. Please use the time to catch up on labs. (Use Tutors, email, and my office hours to get help if you can't complete a problem.) * For Monday, read 4.3-4.6. * Given that I'm giving you time to catch up on labs, I may drop my promise not to talk, particularly given that there are some interesting questions. * EC for soccer on Saturday and Sunday (1:30 or so) * EC for Ren Fair 2-5 on Saturday * EC for Singers at 2 on Saturday in S-L * EC for A Cappella at 4 or 5 on Saturday in Herrick * EC for Free Web somewhere. Ask Asa's little brother Basics of Functions * Three key aspects to a function: Definition, Use, and Declaration * What does a function definition look like in Scheme? Hmmm Um ()()(((((()))()((NAME)((((LAMBDA))))))) DAMN!) That's a big negative You all retroactively fail 151 (define NAME (lambda (PARAMS) BODY)) - DB was close * What does a function definition look like in C? RETURN TYPE NAME (PARAMETERS) { CODE } * What are the differences between the ways we define the parameters in C and Scheme? C requires types, Scheme does not * They also differ in how they use local variables, but that's not the question I asked, and I DISCOURAGE INITIATIVE! * Where do we put the definition? int like_squared(int awesome); int main () { printf ("%d", like_squared (5)); } int like_squared (int awesome) { return awesome * awesome; } // like_squared * We explicitly use "return" in C, not in Scheme * What does return do? * It doesn't print it or anything like that, but it says "This is the output of my function" which another function can use.a * Return also exits the function immediately * How do we call a function * In Scheme? (NAME PARAMETERS), separate params with spaces * In C? NAME(PARAMETERS) separate params with commas * How do we declare a function * In Scheme? * In C? Before you can call a function, you should either define it or declare it Like a definition, but without the body. * Your questions on declaratiosn * Do we have to write the parameters in the definition if there's a declaration? Yes * Do the parameter names have to match? Can I use fred in the declaration and x in the defintiion? Yes * Do we really need to give parameters in the declaration? No, but the C compiler is nicer if you do. * Can we just give the types in the declaration, and avoid the names altogether? Yes * Why bother? * May help with organization. We can group similar functions together w/o worrying about calling order. * Sometimes we have mutually recursive functions * Why not in Scheme? * Multiple possible return types, so we would expect the language to return values * Scheme is a bit more casual than C. Questions: * So, external variables are "globally accessible" + What does globally accessible mean? - It's accessible anywhere + How exactly does this work? Do the function and the declared variable have to be in the same code, or directory, or put in a library that has to be included? * In the same final program, so from anywhere that knows about it. If a library provides function foo() or variable bar, than any code that uses the library gets access to it. * There's a benefit to being able to keep the variable permanent * In the RPN calculator, the array/stack could be global and we could recurse through * static will help with the same sisue, and ewe'll come back to it * I'm having a little trouble understanding why the atoi code on pg. 73 works. int atoi (char s[]) { double atof (char s[]); return (int) atof (s); } // atoi We've already defined atof (or at least K&R have defined atof). The (int) "operation " converts a double into an int We call this conversion "casting" * I don't entirely understand how getch and ungetch store and deal with chars. (x2) We'll cover this when we do file I/O. * I could not understand the for loop in the function strindex on page 69. How does it work when there are two variables in the for condition? for (i = 0; s[i] != '\0'; i++) { for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++) ; if (k > 0 && t[k] == '\0') return i; } * How do void functions work? We'll talk about it later