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 I began to execute the Baked Chicken card, I would discover that the second instruction is Make Seasoned Bread Crumbs, which is explained on another card. A programmer would say the Baked Chicken function calls the Seasoned Bread Crumbs function.

Figure 5.2  Recipe cards

Recipe cards

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

While you are preparing the seasoned bread crumbs, you stop executing the Baked Chicken card. When the bread crumbs are ready, you resume working through the Baked Chicken card.

Similarly, the main function stops executing and blocks until the function it calls is done executing. To see this happen, we’re going to call a sleep function that does nothing but wait a number of seconds. In your main function, add a call to sleep.

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​(​"​M​a​r​k​"​,​ ​"​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​H​P​ ​a​n​d​ ​P​o​s​t​g​r​e​S​Q​L​"​,​ ​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​(​"​T​e​d​"​,​ ​"​i​O​S​"​,​ ​5​)​;​

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

Build and run the program. (Ignore the warning about an implicit declaration for now.) You should see a 2-second pause between each message of congratulations. That’s because the main function stops running until the sleep function is done sleeping.

Notice that when you call a function, you use its name and a pair of parentheses for its arguments. Thus, when we talk about functions, we usually include a pair of empty parentheses. From now on, we will say main() when we talk about the main function.

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

At the top of main.c, you included the file stdio.h. This file contains a declaration of the function printf() and lets the compiler check to make sure that you are using it correctly. The function sleep() is declared in stdlib.h. Include that file, too, so that the compiler will stop complaining that sleep() is implicitly declared:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​
#​i​n​c​l​u​d​e​ ​<​s​t​d​l​i​b​.​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​)​
{​
 ​ ​ ​ ​…​

The standard libraries serve two functions:

  • They represent big chunks of code that you don’t 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’ll learn how to browse the documentation for iOS and Mac 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