Difference between revisions of "Program Structure"

From Lazarus wiki
(New page: Basics 1A - Program Structure (author: Tao Yue, state: unchanged) The basic structure of a Pascal program is: <font color="#000000"> 1: </font><font color="#006699"><strong>PROGRAM<...)
 
Line 2: Line 2:
  
 
The basic structure of a Pascal program is:
 
The basic structure of a Pascal program is:
 +
<delphi>
 +
PROGRAM ProgramName (FileList);
  
  <font color="#000000">  1: </font><font color="#006699"><strong>PROGRAM</strong></font> ProgramName <font color="#000000"><strong>(</strong></font>FileList<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
CONST
  <font color="#000000">  2: </font>
+
   (* Constant declarations *)
  <font color="#000000">  3: </font><font color="#006699"><strong>CONST</strong></font>
 
  <font color="#000000">   4: </font>  <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Constant</font><font color="#cc0000"> </font><font color="#cc0000">declarations</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
  <font color="#990066">  5: </font>
 
  <font color="#000000">  6: </font><font color="#006699"><strong>TYPE</strong></font>
 
  <font color="#000000">  7: </font>  <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Type</font><font color="#cc0000"> </font><font color="#cc0000">declarations</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
  <font color="#000000">  8: </font>
 
  <font color="#000000">  9: </font><font color="#006699"><strong>VAR</strong></font>
 
  <font color="#990066">  10: </font>  <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Variable</font><font color="#cc0000"> </font><font color="#cc0000">declarations</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
  <font color="#000000">  11: </font>
 
  <font color="#000000">  12: </font><font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Subprogram</font><font color="#cc0000"> </font><font color="#cc0000">definitions</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
  <font color="#000000">  13: </font>
 
  <font color="#000000">  14: </font><font color="#006699"><strong>BEGIN</strong></font>
 
  <font color="#990066">  15: </font>  <font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Executable</font><font color="#cc0000"> </font><font color="#cc0000">statements</font><font color="#cc0000"> </font><font color="#cc0000">*)</font>
 
  <font color="#000000">  16: </font><font color="#006699"><strong>END</strong></font><font color="#000000"><strong>.</strong></font>
 
  
The elements of a program must be in the correct order, though some may be omitted if not needed. Here's a program that does nothing, but has all the required elements:
+
TYPE
 +
  (* Type declarations *)
 +
 
 +
VAR
 +
  (* Variable declarations *)
  
  <font color="#000000">  1: </font><font color="#006699"><strong>program</strong></font> DoNothing<font color="#000000"><strong>;</strong></font>
+
(* Subprogram definitions *)
  <font color="#000000">  2: </font><font color="#006699"><strong>begin</strong></font>
 
  <font color="#000000">  3: </font><font color="#006699"><strong>end</strong></font><font color="#000000"><strong>.</strong></font>
 
  
 +
BEGIN
 +
  (* Executable statements *)
 +
END.
 +
</delphi>
 +
The elements of a program must be in the correct order, though some may be omitted if not needed. Here's a program that does nothing, but has all the required elements:
 +
<delphi>
 +
program DoNothing;
 +
begin
 +
end. </delphi>
 
Comments are portions of the code which do not compile or execute. Pascal comments start with a (* and end with a *). You cannot nest comments:
 
Comments are portions of the code which do not compile or execute. Pascal comments start with a (* and end with a *). You cannot nest comments:
  <font color="#cc0000">   {  (* (* *) *)</font>
+
<delphi>
 +
      {  (* (* *) *)
 +
</delphi>
 
will yield an error because the compiler matches the first '<tt>(*</tt>' with the first '<tt>*)</tt>', ignoring the second '<tt>(*</tt>' which is between the first set of comment markers. The second '<tt>*)</tt>' is left without its matching '<tt>(*</tt>'. This problem with begin-end comment markers is one reason why many languages use line-based commenting systems.
 
will yield an error because the compiler matches the first '<tt>(*</tt>' with the first '<tt>*)</tt>', ignoring the second '<tt>(*</tt>' which is between the first set of comment markers. The second '<tt>*)</tt>' is left without its matching '<tt>(*</tt>'. This problem with begin-end comment markers is one reason why many languages use line-based commenting systems.
  
Line 35: Line 36:
  
 
In addition, comments are often used to take problematic code out of action without deleting it. Remember the earlier restriction on nesting comments? It just so happens that braces <tt>{}</tt> take precedence over parentheses-stars <tt>(* *)</tt>. You will not get an error if you do this:
 
In addition, comments are often used to take problematic code out of action without deleting it. Remember the earlier restriction on nesting comments? It just so happens that braces <tt>{}</tt> take precedence over parentheses-stars <tt>(* *)</tt>. You will not get an error if you do this:
  <font color="#cc0000">   {</font><font color="#cc0000"> </font><font color="#cc0000">(*</font><font color="#cc0000"> </font><font color="#cc0000">Comment</font><font color="#cc0000"> </font><font color="#cc0000">*)</font><font color="#cc0000"> </font><font color="#cc0000">}</font>
+
<delphi>
 
+
{ (* Comment *) }
 +
</delphi>
  
 
Whitespace (spaces, tabs, and end-of-lines) are ignored by the Pascal compiler unless they are inside a literal string. However, to make your program readable by human beings, you should indent your statements and put separate statements on separate lines. Indentation is often an expression of individuality by programmers, but collaborative projects usually select one common style to allow everyone to work from the same page.
 
Whitespace (spaces, tabs, and end-of-lines) are ignored by the Pascal compiler unless they are inside a literal string. However, to make your program readable by human beings, you should indent your statements and put separate statements on separate lines. Indentation is often an expression of individuality by programmers, but collaborative projects usually select one common style to allow everyone to work from the same page.

Revision as of 15:38, 5 January 2010

Basics 1A - Program Structure (author: Tao Yue, state: unchanged)

The basic structure of a Pascal program is: <delphi> PROGRAM ProgramName (FileList);

CONST

 (* Constant declarations *)

TYPE

 (* Type declarations *)

VAR

 (* Variable declarations *)

(* Subprogram definitions *)

BEGIN

 (* Executable statements *)

END. </delphi> The elements of a program must be in the correct order, though some may be omitted if not needed. Here's a program that does nothing, but has all the required elements: <delphi> program DoNothing; begin end. </delphi> Comments are portions of the code which do not compile or execute. Pascal comments start with a (* and end with a *). You cannot nest comments: <delphi>

     {  (* (* *) *)

</delphi> will yield an error because the compiler matches the first '(*' with the first '*)', ignoring the second '(*' which is between the first set of comment markers. The second '*)' is left without its matching '(*'. This problem with begin-end comment markers is one reason why many languages use line-based commenting systems.

Turbo Pascal and most other modern compilers support brace comments, such as {Comment}. The opening brace signifies the beginning of a block of comments, and the ending brace signifies the end of a block of comments. Brace comments are also used for compiler directives.

Commenting makes your code easier to understand. If you write your code without comments, you may come back to it weeks, months, or years later without a guide to why you coded the program that way. In particular, you may want to document the major design of your program and insert comments in your code when you deviate from that design for a good reason.

In addition, comments are often used to take problematic code out of action without deleting it. Remember the earlier restriction on nesting comments? It just so happens that braces {} take precedence over parentheses-stars (* *). You will not get an error if you do this: <delphi> { (* Comment *) } </delphi>

Whitespace (spaces, tabs, and end-of-lines) are ignored by the Pascal compiler unless they are inside a literal string. However, to make your program readable by human beings, you should indent your statements and put separate statements on separate lines. Indentation is often an expression of individuality by programmers, but collaborative projects usually select one common style to allow everyone to work from the same page.

previous contents next