Comments are pieces of the source code which are completely discarded by the compiler. They exist only for the benefit of the programmer, so he can explain certain pieces of code. For the compiler, it is as if the comments were not present.
The following piece of code demonstrates a comment:
(* My beautiful function returns an interesting result *) Function Beautiful : Integer;
The use of (* and *) as comment delimiters dates from the very first days of the Pascal language. It has been replaced mostly by the use of { and } as comment delimiters, as in the following example:
{ My beautiful function returns an interesting result } Function Beautiful : Integer;
The comment can also span multiple lines:
{ My beautiful function returns an interesting result, but only if the argument A is less than B. } Function Beautiful (A,B : Integer): Integer;
Single line comments can also be made with the // delimiter:
// My beautiful function returns an interesting result Function Beautiful : Integer;
The comment extends from the // character till the end of the line. This kind of comment was introduced by Borland in the Delphi Pascal compiler.
Free Pascal supports the use of nested comments. The following constructs are valid comments:
(* This is an old style comment *) { This is a Turbo Pascal comment } // This is a Delphi comment. All is ignored till the end of the line.
The following are valid ways of nesting comments:
{ Comment 1 (* comment 2 *) } (* Comment 1 { comment 2 } *) { comment 1 // Comment 2 } (* comment 1 // Comment 2 *) // comment 1 (* comment 2 *) // comment 1 { comment 2 }
The last two comments must be on one line. The following two will give errors:
// Valid comment { No longer valid comment !! }
and
// Valid comment (* No longer valid comment !! *)
The compiler will react with a “invalid character” error when it encounters such constructs, regardless of the -Mtp switch.
Remark In TP and Delphi mode, nested comments are not allowed, for maximum compatibility with existing code for those compilers.