|
June 25th - To live is to Function^-- the above title is a quote by Oliver Wendell Holmes, Jr., and should become
immediately obvious if you actually did the reading for today's class. =) (If you
still don't get it.. look up p.137 in your book.)
We will start our discussion on functions today. In this lecture we will
introduce you the ideas of functions, what they can be used for, why they must be
used, and how.
What are functions?
You should be somewhat familiar or at least aware of what functions are by now, after having done the reading assignment on chapter 5. A function in programming is similar to a math function in a way, it takes in some variable(s), and return a value. The variables that it takes in are called arguments or parameters, and the value that it returns is called return value. You can only have one return value for a function, just like a math function. Conceptually, when you call a function you are basically replacing that line of
code (where you call the function) with what you have in the function body in that
function's declaration. This should give you a hint why it'd be a good idea to use
functions in your program. (Portions of code that are long and/or are achieving
one single purpose and/or need to be executed many times) And of course, most
importantly, this lets you do your program in a top-down design (or
stepwise refinement as your textbook likes to call it). This is a very
important concept and is especially emphasized in college programming and real-world
programming.
Functions need to be declared before they can be called to use. Functions
like printf(), GetInteger() can be used without you declaring them yourself is because
they are included in the libraries that you include in your programs, and this was
explained last week during lectures. To declare a function means to define what it is,
and what it does. Functions need to have prototypes is because programming is
sequential (another topic that I need to talk about, as I notice some people are not
getting this concept). If you are calling a function before your definition of the
function appears, the computer isn't going to know what it is at all -- that is why
you need a prototype for the function -- to let the computer know there exists such
a function, and that you will define it later.
Syntactical issues
When calling a function, there are several rules to follow.
The general syntax of a function prototype for your reference:
DataType FunctionName ( arguments ); The general syntax of a function declaration for your reference:
DataType FunctionName ( arguments ) { // comment: your stuff here } Homework assignment
Finish the program you started on in class, which is problem #2 on page 179 in your textbook. Besides doing what it tells you to, also make it so that the program asks you again whether you want to try again. If the user types in "no", then the program quits, for everything else, it needs to continue, starting over from the beginning again. Hint: Look on page 150 to find a piece of code that can achieve this similar purpose. To use the square root function sqrt(), you will have to use the math library. It has the same path as simpio.h and genlib.h, with a different filename only: #include "/home/cc/atdp4/su03/staff/atdp4-tc/math.h" And the correct syntax for the sqrt() function is: (assume x and y are of type doubles) x = sqrt(y); // this sets x equal to the square root of y Remember, make a function to solve the quadratic equation, rather than doing it in main()! Make a function called, for instance, QuadEquations() that takes in three variables, that are of type double. The function does not have to return anything, because you could simply printf() out the results from inside the function. Read Chapter 11, on Arrays. |