Comment
Learn how to use Comments and why you should always use them
Single Line Comment
In C++, a single comment begins with a double slash (//) and continue till the ends of the line
//This is a comment int my_number = 128; //This is another comment the comment continue here and forever
Multi-Lines Comment
First thing to know is that a multi-lines comment does not necessary have multiple lines. The name simply refers to the fact that you can comment multiple lines with it if you want.
In C++, a multi-lines comment begins with a slash-asterisk (/*) and ends with a asterisk-slash (*/), The multi-lines comment can be used anywhere in the code, I really mean any where, even between a variable type and its name when declaring it.
/* This is a multi-lines comment Line 1 ... Line N */ /* this comment is only one line, it does not continue forever, it ends with the asterisk-slash */ int my_number = 128; std::string /*this is a comment*/ my_string = "hello everyone";
Why Comments Are Important
Comments are useful but also very important and you should learn to use them instinctively every time. Comments are not part of your program, if you take a program with some comments and remove them all, your program will still work the same. So why do we use them ?
Because they allows you do describe what your program is doing at any moment in a very human friendly way. Also If you are working with other peoples you want them to know what your code is doing just by reading it. Even if you work alone, it may happen that you stop working on your code for several months, without comments you will have an hard time understating your own code when come back to it.
As you can see with the examples below, code with comments are more easy to understand.
//Example 1 : without comment float surf(float w, float h) { return w * h; } //Example 2 : with comments /* The surf function computes the surface of any rectangulare shape given its width and its height w = width h = height */ float surf(float w, float h) { return w * h; //return surface }