Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\texttt} \newcommand{\hex}{\texttt} \newcommand{\octal}{\texttt} \newcommand{\prog}{\texttt} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Chapter10Structure of the main Function

Essentially all programs are broken down into smaller units, each dedicated to performing a specific task. In C such a unit is called a function. Other names are subroutine, routine, procedure, or subprogram. We will use the C terminology, “function,” to discuss subroutines. Regardless of the name, these chunks of code make up a callable unit that follows the general pattern:

  1. The calling function passes zero or more arguments (pieces of data) to the called function.

  2. The called function uses this input data as parameters to perform its action.

  3. The called function returns program flow back to the calling function, possibly providing a returned data value.

In Section 2.5 you learned that all C programs begin with a function named main, which you can think of as a subroutine that is called by the operating system. In this chapter you will learn how the main function is organized (which is typical of all functions in C, and how you can call other functions from main. You will also learn how a function allocates memory for its own use while it is being executed. In Chapter 13 you will learn how to write functions that are called by main.