How functions work together

A program is a collection of functions. When you run a program, those functions are copied from the hard drive into memory, and the processor finds the function called main and executes it.

Remember that a function is like a recipe card. If you began to execute the Easy Broiled Chicken card, you would discover that the third instruction says Execute the Seasoned Bread Crumbs recipe, which is explained on another card. A programmer would say, The Easy Broiled Chicken function calls the Seasoned Bread Crumbs function.

Figure 5.1  Recipe cards

Recipe cards

Similarly, main() can call other functions. For example, main() in ClassCertificates called the congratulateStudent(), which in turn called printf().

While you are preparing the bread crumbs, you stop executing the steps on the Easy Broiled Chicken card. When the bread crumbs are ready, you resume working through the Easy Broiled Chicken card.

Similarly, the main function stops executing and blocks until the function it called is done executing. To see this happen, you are going to call a sleep function that does nothing but wait a number of seconds. This function is declared in the file unistd.h. At the top of main.c, include this file:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​
#​i​n​c​l​u​d​e​ ​<​u​n​i​s​t​d​.​h​>​

v​o​i​d​ ​c​o​n​g​r​a​t​u​l​a​t​e​S​t​u​d​e​n​t​(​c​h​a​r​ ​*​s​t​u​d​e​n​t​,​ ​c​h​a​r​ ​*​c​o​u​r​s​e​,​ ​i​n​t​ ​n​u​m​D​a​y​s​)​
{​
…​

In your main function, call the sleep function after the calls to congratulateStudent().

i​n​t​ ​m​a​i​n​ ​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​c​o​n​g​r​a​t​u​l​a​t​e​S​t​u​d​e​n​t​(​"​K​a​t​e​"​,​ ​"​C​o​c​o​a​"​,​ ​5​)​;​
 ​ ​ ​ ​s​l​e​e​p​(​2​)​;​
 ​ ​ ​ ​c​o​n​g​r​a​t​u​l​a​t​e​S​t​u​d​e​n​t​(​"​B​o​"​,​ ​"​O​b​j​e​c​t​i​v​e​-​C​"​,​ ​2​)​;​
 ​ ​ ​ ​s​l​e​e​p​(​2​)​;​
 ​ ​ ​ ​c​o​n​g​r​a​t​u​l​a​t​e​S​t​u​d​e​n​t​(​"​M​i​k​e​"​,​ ​"​P​y​t​h​o​n​"​,​ ​5​)​;​
 ​ ​ ​ ​s​l​e​e​p​(​2​)​;​
 ​ ​ ​ ​c​o​n​g​r​a​t​u​l​a​t​e​S​t​u​d​e​n​t​(​"​L​i​z​"​,​ ​"​i​O​S​"​,​ ​5​)​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run the program. You should see a two-second pause between each message of congratulations. That is because the main function stops running until the sleep function is done sleeping.

Standard libraries

Your computer came with many functions built-in. Actually, that is a little misleading – here is the truth: Before OS X was installed on your computer, it was nothing but an expensive space heater. Among the things that were installed as part of OS X were files containing a collection of precompiled functions. These collections are called the standard libraries.

Two of the files that make up the standard libraries are stdio.h and unistd.h. When you include these files in your program, you can then use the functions that they contain. printf() is in stdio.h; sleep() is in unistd.h.

The standard libraries have two purposes:

  • They represent big chunks of code that you do not need to write and maintain. Thus, they empower you to build much bigger, better programs than you would be able to do otherwise.

  • They ensure that most programs look and feel similar.

Programmers spend a lot of time studying the standard libraries for the operating systems that they work on. Every company that creates an operating system also has documentation for the standard libraries that come with it. You will learn how to browse the documentation for iOS and OS X in Chapter 16.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset