The cornerstone of C and C++ programming can be described in one word functions. This chapter introduces the concept of a function and how it is prototyped under the latest ANSI C standard. Many example programs will be used to examine the different types of functions and how arguments are passed. You will also learn how to use the standard C/C++ variables argc and argv to pass command-line arguments to the main( ) function. Additionally, this chapter explores several unique features available in C++.
Functions form the main building blocks of most C and C++ programs. Functions allow you to separate and code parts of your program in separate modules. Thus, functions allow your program to take on a modular appearance. Modular programming allows a program to be separated into workable parts that contribute to a final program form. For example, one function might be used to capture input data, another to print information, and yet another to write data to the disk. As a matter of fact, all C and C++ programming is done within a function. The one function every C or C++ program has is main( ).
You will find that C and C++ functions are similar to programming modules in other languages. For example, Pascal uses functions and procedures, while FORTRAN uses just functions. The proper development of functions determines, to a great extent, the efficiency, readability, and portability of your program code.
This chapter contains many programming examples that will show you how to create and implement a wide range of functions. Many of the example programs also use built-in C and C++ library functions that give your program extended power.