A Closer Look at C Functions
Functions are small routines or software commands that do specific tasks. In the C language, most of the work is done by these functions, not by the C language keywords. Functions are the key to creating a powerful program in the C language.
Despite having only 32 keywords, the C language has hundreds of functions. Most programs use a common set of about 50 or so. But — and this is a good thing — there are dozens and dozens of additional functions, many of which do amazing and specific things that you can incorporate into your programs.
Different types of C functions
Functions work in several ways. First, they can be used to immediately carry out a task. For example, the abort() function is used to quit your program. It doesn’t generate a result, nor does it process any value or information:
This command immediately ends a program (assuming that some error condition has occurred). More importantly, it’s a rare example of a C language function that doesn’t process any information.
A second type of function sends information to the computer’s hardware or operating system. For example, the puts() function is used to display a bit of text on the screen:
When this function is run, Hello! appears on your screen.
Functions can also return information from the computer, telling you what’s going on. The getchar() function, for example, returns a character typed at the keyboard:
key = getchar();
Unlike puts(), getchar() doesn’t require anything between its parentheses. Instead, it merely returns a value that must be stored. In the preceding line, the value is stored or assigned to the key variable.
Finally, some functions take something and give back something in return. The sqrt() function, for example, takes a value and returns that value’s square root:
root = sqrt(27);
This command takes the value 27 and returns that value’s square root, which is then stored in the root variable.
Finding the right C function
Alas, all the C language functions aren’t listed in one place anymore. In the old days, the C compiler’s manual would have a full listing. Today, most of that information is kept in the compiler’s online help system, though you have two other handy ways to look up information about functions:
Visit the gcc home page. You can find ample documentation on that Web site.
If your compiler has its own home page on the Web, consider visiting that site to peruse the documentation.
Remember to bookmark the pages you find!
Finally, as a bonus, users of Unix-like operating systems (FreeBSD, Linux, Mac OS X) can use the man command to look up functions by name. For example:
This command displays the manual entry for the sqrt() function, including its options, common uses, and related or similar functions.
Note that some C functions can also be Unix commands, such as exit and log. The C language functions are defined in Section 3 of the man pages. As an example, to look up the C language log function, use this command:
man 3 log