©1994-2003 Kevin Boone
Home     Section index     K-Zone home C++ tutorial (under construction)

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
define functions
contents
loops
C++ tutorial: standard functions


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:

  • date and time manipulation
  • debugging support
  • error handling
  • file and directory management
  • hardware control
  • input/output operations
  • memory management
  • mathematics
  • process and task management
  • searching and sorting of data
  • text manipulation
C++ goes a step further and introduces standard classes as well as standard functions. You will already be familiar with 'cout' and 'cin'. These are instances of the standard classes 'ostream' and 'istream' (short for 'output stream' and 'input stream'). Unfortunately, at the time of writing details of the standard class library have no been fully standardized, and different compiler manufacturers are still doing things slightly differently.

Is there a function for...?
One of the problems that faces C programmers, both beginners and the more experienced, is finding out whether it is actually necessary to write a particular function. If your required function is reasonably general-purpose, you may find that it is in the standard library, or is similar to one that is. In that case it may be unnecessary to write the function yourself. However, sometimes it is quite difficult to find out whether a function is in the standard library or not. Standard reference books on C/C++ will have the information, but it is often quite difficult to find. This is where on-line help systems have made such an improvment. With modern compiler packages it is possible to search the documentation by keyword, and find if any functions match. For example, if I am interesting in finding a function that will give the current time, I can search for 'time' in the on-line help, and get a list of about 5 functions concerned with time. From there it only takes a click of the mouse to jump to the appropriate page, and see whether the function is relevant.

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
The program math1.cpp demonstrates the use of some simple maths functions from the standard library. The 'sqrt' function calculates square roots. For example, this line

     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
The C++ compiler is very fussy: it will complain if it encounters a function name that it does not recognize, and it always works strictly from top to bottom.

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.