2330 : Introduction to Programming in C

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.
  1. Always open a parenthesis after the function name. If you have no arguments to put in, then just close it afterward. If you do have arguments, then pass them in, separated with commas each. This is why when we programmers mention a function in a paragraph of English text, we put parenthesis after the function name, like printf(). It is to indicate that it is a function, just like you would need to do in your code.
  2. Always set a variable equal to a function, and it has to be the same type as the return value of the function. Unless this function is a void function, in which case it returns nothing, then you don't need to do this.
  3. Type the function name exactly as it should be when you are calling it!! Too many people are neglecting the case-sensitive issue in programming, whether you just don't care enough to or don't pay enough attention. Being careful about the case (not only for functions, as well as variable names, etc.) will save you a LOT of debugging time. Remember, programming is giving instructions to the computer, and the computer does not speak English, if it doesn't know exactly what it is that you're trying to do, it won't do it. This may sound harsh, but programming allows no room for sloppiness. (That is, if you want your program to compile and run fine!) (Well, I suppose you could always make code that's sloppy but syntactically right.. but that's another story)
When declaring a function, there are several rules to follow.
  1. You need a return type. It is the type of the value that your function will return, for example an int, a char, etc. If your function does not return any value, its return type is void.
  2. If your function is not a void function, you will need at least 1 return line somewhere in your function body. It should be obvious why. The return statement will, uh, return the return value. That should be understandable..
  3. Check with the syntax reference below. Pay attention: there is no semi-colon after first line in the declaration, unlike the prototype -- remember the rule with the starting curly brackets?
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.