Section 7j

The main Function


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

int main

The Ultimate Function

So far in this reference, you have been using the main function for your programs. From this point forward, you will be writing a lot of supporting and utilitarian functions. However, you should already know that all (correctly operating) C programs start and end in the main function. When you run your program, the main function is called by the operating system in order to conduct its business.

The operating system does not care what the main function does, as long as it does not try to cause trouble for the system, so your program will run until it is done with its given tasks. Working with individual functions or tasks will bring you closer to what it is like to work in the professional programming environment. The main function is just the one that gets called by the operating system to run the program.

Calling all functions. In C, everything happens inside a function. The main function is called by the operating system, and the whole program works through functions called from that same main function. The main function can call other functions, which can call other functions, and so on. It has been mentioned in a previous topic that this process of calling a variety of other functions or modules to solve the problem or create the solution makes the main function a driver. While all other functions must be designed and implemented in such a way that they focus tightly on specific goals or tasks, the main function is the program and thus, accomplishes many different tasks. Like all the other functions, it does only conduct one activity, but that activity is the program execution.

Functions are the building blocks of any program. They need to be small, reusable, and modular components so they work together to form an easily readable, effective, and correct program. With this in mind, functions and modular development are the only way to handle programming at a large and/or complicated scale, and they can be used to make problem solving and task completion a well managed process. Functions are a powerful tool with which you will continue your learning of Computer Science in the next topics.

This is the last section in the chapter on functions. However, it is only the beginning for your work with functions. In the next chapter, you will put them to work in a carefully structured programming process that will help you learn to develop well-structured and well-organized programs.