C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

program might not require many lines of code, but it’s much better to get in the habit of breaking every
program into distinct tasks.


Note

Breaking programs into smaller functions is called structured programming.

Don’t use main() to do everything. In fact, you should use main() to do very little except call
each of the other functions. A better way to organize this program would be to write separate
functions for each task the program is to do. Of course, not every function should be a single line, but
make sure each function acts as a building block and performs only a single task.


Here is a better outline for the program just described:


Click here to view code image


main()
{
assignID(); // Sets up a unique ID for the new employee
buildContact(); // Enters the employee's basic contact info
payrollAdd(); // Adds the new employee to the payroll system
return 0;
}
/* Second function, one that sets an ID for the new employee
assignID()*/
{
// Block of C code to set up a unique ID for the
// new employee
return;
}
/* Next function—the contact building function */
buildContact()
{
// Block of code to input the employee's
// home address, phone number, birth date,
// and so on
return;
}
/* Fourth function to add employee to the payroll */
payrollAdd()
{
// Code to set the new employee's salary,
// benefits, and other info in the
// payroll system
return;
}
Free download pdf