So what is a program?

Now that you’ve built and run your program, let’s take a look inside. A program is a collection of functions. A function is a list of operations for the processor to execute. Every function has a name, and the function that you just wrote is named main. There was also another function – printf. You didn’t write this function, but you did use it. (We’ll find out where printf comes from in Chapter 5.)

To a programmer, writing a function is a lot like writing a recipe: Stir a quart of water slowly until it boils. Then mix in a cup of flour. Serve while hot.

In the mid-1970’s, Betty Crocker started selling a box containing a set of recipe cards. A recipe card is a pretty good metaphor for a function. Like a function, each card has a name and a set of instructions. The difference is that you execute a recipe, and the computer executes a function.

Figure 2.5  A recipe card named Baked Chicken

A recipe card named Baked Chicken

Betty Crocker’s cooking instructions are in English. In the first part of this book, your functions will be written in the C programming language. However, a computer processor expects its instructions in machine code. How do we get there?

When you write a program in C (which is relatively pleasant for you), the compiler converts your program’s functions into machine code (which is pleasant and efficient for the processor). The compiler is itself a program that is run by Xcode when you press the Run button. Compiling a program is the same as building a program, and we’ll use these terms interchangeably.

When you run a program, the compiled functions are copied from the hard drive into memory, and the function called main is executed by the processor. The main function usually calls other functions. For example, your main function called the printf function. (We’ll see more about how functions interact in Chapter 5.)

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

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