CS/CE 218 Lecture -*- Outline -*- Connection: now we'll start to move from the small scale issues of writing C code to larger scale issues for a time. We'll come back to small scale issues in a bit, but for now we will focus on issues that are complimentary to those we studied at the end of the shell programming session: issues of program organization. These are by far the most important, as you wouldn't have too much trouble with the small scale ones even if you didn't learn them in this class. * C Functions Advert: functions are the most basic abstraction mechanism in C. function declaration an area of change in ANSI C Q: Why is it a good idea to declare function arguments properly? so the compiler can give better error messages so the compiler do automatic coercions ** basics (section 4.1) C doesn't distinguish between functions and procedures, as in Pascal. As an expression language, all C functions return a value the value "nothing" has type "void" *** syntax Q: What's the syntax of a C function declaration? () { } --------------- int T(int x) { return (odd(x) ? (3*x+1) : x) / 2; } --------------- the body can be null (for a stub) Q: What is the default return type for a function? int best to always declare it Should always return a value of the appropriate type (unless type is void) *** load and go compilation Q: If you have files main.c, getline.c, and strindex.c how do you compile them all at once? gcc main.c getline.c strindex.c ** fuction types (section 4.2) functions can return values of types other than int much as in Pascal, what's returned has to be a pointer or some sort of scalar (int, float, double, ...) ---------------------- #include void error(char name[], char msg[]) { fprintf(stderr, "%s: %s\n", name, msg); exit(1); } ---------------------- explain the above formals, types, etc. void is like "nothing" To declare a use of a function, can simply copy the definition heading, optionally taking out argument names Q: How would you declare the above function (to use it somewhere else)? void error(char name[], char msg[]); void error(char [], char []); Q: Can functions be implicitly declared? Is that a problem? yes, can lead to type errors Q: How should you declare a function that takes no arguments? have to say double pi(void); if you write double pi(); then the compiler doesn't do type checking on arguments (old style) Q: How would you declare a function that takes no arguments, and returns no results? void side_effect(void); what would it be good for? ** recursion (section 4.10) C functions can be recursive (just like Pascal, unlike Fortran) --------------- int T(int x) { if (odd(x)) { return T(3*x+1); } else { return x/2; } } --------------- Q: What are the advantages of writing a recursive function? match to recursive problem statement code easier to write and understand then code may be more compact Q: What are some disadvantages of recursion? no storage savings, since maintains stack of automatic vars How important are these disadvantages? not very, except in the most time-critical applications Q: Can you do exercise 4-12 or 4-13 on page 88?