Programming
A computer program works the same way as any other sequential process. One command is implemented after the other. For example, supposed a programmer wanted to create a picture with three asterisks, and he has a printAsterisk command. The program in its simplest form would look like the following in C:
int main()
{ // print first asterisk
// function: printAsterisk
printAsterisk();// print next asterisk
// function: printAsterisk
printAsterisk();// print third asterisk
// function: printAsterisk
printAsterisk();//return value
return 0;
}
As one might might expect, there is much more required of a complete program, however this is a good example of the code that makes a program work. Take a look at the components of this program:
- The program starts with an int main(). All programs will have one and only one main function, and this is the actual program from beginning to end; as mentioned, there is more to writing the complete programs, but this is the core component of a program. To expand on this code, the word int is the type that the program must return at the end. Finally, the name main is an identifier naming the function, and in some cases, if a user calls this main function, she could put other arguments or identifiers on the same line (called a command line) as the class name and they would be brought into the program. The explanation of these items will help with the goal in figuring out how a program works.
- The program code is enclosed in curly braces that are indented as shown; this indicates all of the operations related to the main function; some tips for using the curly braces:
- indent them three spaces
- never type one curly brace without the other; when creating the main function now, and other functions later, always type the header line which is int main() in this case, then press ENTER, type three spaces, type the open curly brace, then press ENTER two or three times to make some vertical spaces, then type three spaces and a close curly brace; getting into this simple habit now will prevent hours chasing missing curly braces later
- After the main function and curly braces are typed in, type the planned actions in as comments. Comments that go to the end of only one line are created by typing two forward slashes; so in this case, type // print first asterisk, start a new line and type // print second asterisk, start a new line and type // print third asterisk; at this point, the program has been written, but it has been written in English; the development process that will be used later on will include primarily typing the actions in English first, and then later translating them to code; note that comments are used for developing and clarifying what a program does, but they are ignored by the compiler so they do not have anything to do with the executable program
- Once the program is written in English, type the code for each operation with proper syntax; type the command exactly as spelled, then type an empty set of parentheses, and then add a semicolon to the end; some notes:
- the command is an identifier that conducts a specified operation; in this case it is a printAsterisk function that was written in advance; this is assumed to be a command, but from now on it will be called a function
- the open and closed parentheses are required for any function used in a program; to repeat, functions conduct many operations in programming, so they are treated as commands; and to repeat again, all functions require open and closed parentheses; as will be presented soon, there will be times when the parentheses will have things between them
- the semicolon announces to the C compiler that the program statement is complete; this is a syntactical requirement of the C programming language
- the code that takes care of printing an asterisk is a little more complicated than this one command; however, the printAsterisk function is an abstraction that covers up the details of conducting the task; the program is broken into contributing components called modules using this function, so the program is considered to be modular; this means the program is easier to develop by using modules, it is easier to understand and use since it is easy to see what each part does; and it will always be easier to diagnose and repair if anything goes wrong because if there is a failure, it is usually easy to see which module is causing the problem
- This discussion is simply showing how a program looks; however, in order to check programming code during the programming process, it is wise to compile the program after every statement; no more than one or two statements should be written at a time without compiling; the compiler checks the code syntax and indicates if something was missed or incorrectly implemented related to one or more of the rules of the programming language. This means the code can be fixed quickly and easy while it is still being worked on; programmers who write a bunch of code first and then compile should not be surprised at how many errors will pop up, and they will waste a lot of time trying to figure out where they are, and what they are
- Finally, notice that the program is simple and easy to read; the operations are clear and understandable; and white space is used to help keep the code readable; white space simply means that the code is not all jammed together and is easier to read; no programmer can guarantee a perfectly running program, however any programmer can guarantee easily readable, easily understandable, and well-developed programs
Again with consideration for the fact that there is a little more to the program than just the main function, the program written here simply outputs three asterisks to what is called the console window.
The results of the program previously discussed would be (as it would be found in the console window):
The above graphic shows the display in the console window. However, for reference, note that the coding area is above it, and in this case, it shows the program that was discussed.
Don't just watch this video, try it yourself. You will need the asterisks.h file so you can download it here
The only other trick to writing these functions (aka commands) is that the parentheses can be helpful. When the three printAsterisk() functions were written, three asterisks were displayed. But there is sometimes an easier way. There might be a function that can print more than one asterisk; if there was, it might be plural, as in printAsterisks, and it would actually have a parameter (i.e., some data in between the parentheses that can be used to give the function the information it needs to do its job). Now, one function can do the job of several, as shown.
Don't just watch this video, try it yourself.
There will be much more discussion about functions and parameters as you progress through this reference. However, by looking at what you can do right now with the tools you are given, it means you can start writing programs immediately. Getting started with the programming will get you moving toward the more advanced activities.