Programming in C - Functions and Modular Programming
(Page 7 of 9 )
The word “function” has appeared a few times so far in this chapter with reference to main(), printf(), function body, and so on. Let’s explore in a little more depth what functions are and why they’re important.
Most programming languages, including C, provide a way of breaking up a program into segments, each of which can be written more or less independently of the others. In C these segments are called functions. The program code in the body of one function is completely insulated from that of other functions. A function will have a specific interface to the outside world in terms of how information is transferred to it and how results generated by the function are transmitted back from it. This interface is specified in the first line of the function, where the function name appears.
Figure 1-3 shows a simple example of a program to analyze baseball scores that is composed of four modules.

Figure 1-3. Modular programming
Each of the four modules does a specific, well-defined job. Overall control of the sequence of operations in the program is managed by one module, main(). There is a module to read and check the input data and another module to do the analysis. Once the data has been read in and analyzed, a fourth module has the task of outputting the team and player rankings.
Segmenting a program into manageable chunks is a very important aspect to programming, so let’s go over the reasons for doing this:
- Dividing the program into a number of separate functions allows each function to be written and tested separately. This greatly simplifies the process of getting the total program to work.
- Several separate functions are easier to handle and understand than one huge function.
- Libraries are just sets of functions that people tend to use all the time. Because they’ve been prewritten and pretested, you know they’ll work, so you can use them without worrying about their code details. This will accelerate your program development, by allowing you to concentrate on your own code, and it’s a fundamental part of the philosophy of C. The richness of the libraries greatly amplifies the power of the language.
- You can accumulate your own libraries of functions that are applicable to the sorts of programs that you’re interested in. If you find yourself writing a particular function frequently, you can write a generalized version of it to suit your
18 needs and build this into your own library. Then, whenever you need to use that particular function, you can simply use your library version.
•In the development of very large programs, which can vary from a few thousand to millions of lines of code, development can be undertaken by teams of programmers, with each team working with a defined subgroup of the functions that make up the whole program.
You’ll learn about C functions in greater detail in Chapter 8. Because the structure of a C program is inherently functional, you’ve already encountered one of the standard library functions in one of this chapter’s earliest examples: the function printf().
This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now. |
Next: Try It Out: Exercising What You Know >>
More Code Examples Articles
More By Apress Publishing