| Previous | Table of Contents | Next |
![]() | There are two types of comments in C++. The double-slash (//) comment, which will be referred to as a C++-style comment, tells the compiler to ignore everything that follows the slashes until the end of the line. |
The slash-star (/*) comment mark tells the compiler to ignore everything that follows until it finds a star-slash (*/) comment mark. These marks will be referred to as C-style comments because C++ inherited them from C. Remember, every /* must be matched with a closing */.
Many C++ programmers use the C++-style comment most of the time, and reserve C-style comments for blocking out large blocks of a program. You can include C++-style comments within a block commented out by C-style comments; everything, including the C++-style comments, is ignored between the C-style comment marks.
Comments are free; they dont cost anything in performance, and they are ignored by the compiler. Listing 2.2 illustrates this.
Listing 2.2 HELLO.CPP Demonstrates Comments
1: #include <iostream.h>
2:
3: int main()
4: {
5: /* this is a comment
6: and it extends until the closing
7: star-slash comment mark */
8: cout << Hello World!\n;
9: // this comment ends at the end of the line
10: cout << That comment ended!;
11:
12: // double slash comments can be alone on a line
13: /* as can slash-star comments */
14: return 0;
15: }
![]() | Hello World! That comment ended! |
![]() | The comments on lines 5 through 7 are completely ignored by the compiler, as are the comments on lines 9, 12, and 13. The comment on line 9 ends with the end of the line; however, the comments on 5 and 13 require a closing comment mark. |
Writing comments well is a skill few programmers master.
It is best to assume your audience can read C++, but cant read your mind. Let the source code tell what you are doing, and use comments to explain why you are doing it.
While main() is a function, it is an unusual one. Your operating system invokes main() to start your program. Other functions are called, or invoked, from main() or from one another during the course of your program.
The function main() always returns an int, which is a C++ term for integer. As youll see in the coming hours, other functions might return other types of values or might return nothing at all.
A program is executed line by line in the order it appears in your source code, until a function is called. Then the program branches off to execute the function. When the function finishes, it returns control to the next line in the calling function.
Imagine that you are drawing a picture of yourself. You draw the head, the eyes, the nose, and suddenly your pencil breaks. You branch off to the sharpen my pencil function. That is, you stop drawing, get up, walk to the sharpener, sharpen the pencil, and then return to what you were doing, picking up where you left off. (I think you were adding a cleft to your chin.)
When a program needs a service performed, it calls a function to perform the service. When the function returns, the program resumes where it was just before the function was called.
Listing 2.3 demonstrates this idea.
Listing 2.3 Demonstrating a Call to a Function
1: #include <iostream.h>
2:
3: // function Demonstration Function
4: // prints out a useful message
5: void DemonstrationFunction()
6: {
7: cout << In Demonstration Function\n;
8: }
9:
10: // function main - prints out a message, then
11: // calls DemonstrationFunction, then prints out
12: // a second message.
13: int main()
14: {
15: cout << In main\n ;
16: DemonstrationFunction();
17: cout << Back in main\n;
18: return 0;
19: }
![]() | In main In Demonstration Function Back in main |
![]() | The function DemonstrationFunction() is defined on lines 58. When it is called, it prints a message to the screen and then returns. |
Line 13 is the beginning of the actual program. On line 15, main() prints out a message saying it is in main(). After printing the message, line 16 calls DemonstrationFunction(). This call causes the commands in DemonstrationFunction() to execute. In this case, the entire function consists of the code on line 7, which prints another message. When DemonstrationFunction() completes (line 8), it returns to where it was called from. In this case, the program returns to line 17, where main() prints its final line.
Functions either return a value, or they return void, meaning they return nothing. A function that adds two integers might return the sum, and thus would be defined as returning an integer value. A function that just prints a message has nothing to return and would be declared to return void.
Functions consist of a header and a body. The header consists, in turn, of the return type, the function name, and the parameters to that function. The parameters to a function allow values to be passed into the function. Thus, if the function were to add two numbers, the numbers would be the parameters to the function. Heres a typical function header:
int Sum(int a, int b)
| Previous | Table of Contents | Next |