|
©1994-2003 Kevin Boone |
| Home Section index K-Zone home | C++ tutorial (under construction) |
|
C/C++ defines a large 'standard library' of functions Over the years, it has become apparent that C/C++ programs do many of the same sorts of thing, even when the applications are totally different. For example, almost all programs will take input from the keyboard and display results on a screen of some kind; many will require mathematical operations; many will process text in various ways. Functions to handle these basic operations have been written and collected together in the 'standard library'. At present the standard library contains functions in the following general categories:
Is there a function for...? It is usually much easier to use a standard library function than to write a new one. Even if the function does not do exactly what you need, it is often easier to write a piece of program that modifies the results given by a standard function that to write the function from scratch. Moreover, you should be thinking about re-using your programming work all the time. Often it is very easy to make a function more general purpose than you really need for a given program; then you can use that function again in other jobs. The ability to do this is largely what distinguishes efficient from inefficient programmers.
'Standard library' functions are easy to use in your programs
cout << "The square root of 16 is " << sqrt (16.0) << "\n";
calculates and displays the square root of 16, which is 4. The key part of the statement is the 'sqrt' function call:
sqrt (16.0)
This function takes as its input the number '16' and produces the appropriate number as output. Where does the output go? In this case the output is in the form of a return value. The return value effectively replaces the function call itself when the program is executed. In this case the line:
cout << "the square root of 16 is " << sqrt (16.0) << "\n";
effectively becomes:
cout << "the square root of 16 is " << 4.0 << "\n";
as the result of the 'sqrt' function is fed back into the line as it is executed. For simply function like this, the use of the return value is an intuitive way to pass data out of a function. It leads to expression which make mathematical sense, like
y = sqrt (x) and
HeightOfTriangle = BaseOfTriangle * tan (Angle) We will see in later sections how to define new functions that provide a return value that can be used by the calling program like this.
Use '#include' to tell the compiler that you are going to use standard functions In math1.cpp you may have noticed the line
#include <math.h> At the top of the program. This is an instruction for the compiler to insert the contents of the file 'math.h' at this point. 'math.h' is an example of a header file, i.e., one that is designed to be inserted at the top of a program. math.h does not contain any program instructions as such; it merely defines the functions associated with maths. If you look in the math.h file (on my computer it is in the directory "\program files\bc5\include") you will see a line somehting like this:
double sqrt (double x); This is a function prototype: it simply tells the compiler what format must be used when the function is called. In this case it says that 'sqrt' is a function with one input -- of type 'double' -- and a return value of type 'double'. A large number of header files are associated with the standard library functions; you will eventually learn which ones to include. In the meantime, the documentation that comes with the compiler will tell you, along with details of the function itself. Exercise: Write a program that calculates the square root of a number entered by the user, if it has one (i.e., is positive or zero) and prints an error message if it does not. The program should print an error message also if the user enters something that is not a number. Hint: look at cin3.cpp and math1.cpp for ideas. |