Syntax and Functions Edit This Page

download Syntax and Functions Edit This Page

of 36

Transcript of Syntax and Functions Edit This Page

  • 8/9/2019 Syntax and Functions Edit This Page

    1/36

    Syntax andfunctions edit this page

    [edit ] Syntax As you've seen in the earlier chapter,Pascal programs have a standard structurewhich looks like the following:

    Program program_name;

    { Global variables }Var

    A_Variable: Variable_Type;

    { Other functions/procedures }

    Procedure SayHello;

    { Local variables }Var

    T : String;

    Begin

    { Redundant code to illustrate the useof local variables in a procedure }T := 'Hello';Writeln(T);

    End;

    { Main function }

    Begin{ Do something }SayHello;

    End.

    A program has a program header , followed by global variable definitions , procedureor function definitions and finally the main function .

    [edit ] Variables

    This article is part of the Getting Startedseries

    Beginning

    Syntax and functions

    WRITE and WRITELN

    READ and READLN

    Variables

    Constants

    Numeric variables

    Strings

    Chars

    Boolean variables

    Arrays, matrixes, blocks

    Operations with variables

    String operations

    Sets

    IF...THEN...ELSE

    CASE

    REPEAT...UNTIL

    FOR...DO

    WHILE...DO

    BREAK, HALT and GOTO

  • 8/9/2019 Syntax and Functions Edit This Page

    2/36

    As you may already figure, variable definitions are put in a block beginning with avar keyword, followed by definitions of the variables you wish to define. This blockhas no explicit end marker.

    As you can see from the example, unlike C/C++, Pascal variables are declaredoutside the code-body of the function (i.e. they are not declared within the begin andend pairs), but are instead declared after the definition of the procedure/function andbefore the begin keyword. For global variables, they are defined after the program header.

    A declaration in the var block as the following syntax:

    A_Variable,Another_Variable ... : Variable_Type;

    Declarations may occur on multiple lines, as in the following:

    a,b : integer;c : integer;d,e : string;f : real;g : extended;h,i,j,k : byte;l,m,n,o : byte;

    Basic numeric types include: longint (32-bit, hardware dependent), integer (16-bit,hardware dependent), shortint (8-bit). Their unsigned counterparts are cardinal(available only in some versions of Pascal), word , byte . Decimal numbers aresupported using the real type, and the extended type (available only in someversions of Pascal)

    Other types include the char (for holding characters), the string (as its namesuggests).

    (For now, arrays, pointers, types and records will be covered in a later chapter)

    [edit ] Functions /Procedures

    Before we begin, let us first clarify the key difference between functions andprocedures. A procedure is set of instructions to be executed, with no return value. Afunction is a procedure w ith a return value . For readers familiar with C/C++, aprocedure is simply a function with a void return value, as in void proc_sayhello().

  • 8/9/2019 Syntax and Functions Edit This Page

    3/36

    The definition of function/procedures is thus as such:

    F unction F unc_Name(params...) : Return_Value;Procedure Proc_Name(params...);

    The function/procedure definition is usually followed by the local variables and thebody. However, to provide prototypes, simply add a for w ard keyword behind thedefinition instead of the local variables and the body. Of course, the whole functionmust be defined somewhere else in the program. The following example illustratesthe use of this:

    F unction Add(A, B : Integer): Integer; F orward;

    F unction Bad(A, B, C : Integer) : Integer;

    BeginBad := Add(Add(A,B),C);

    End;

    F unction Add(A, B : Integer) : Integer;Begin

    Add := A + B;End;

    In this example, Add is first defined as a function taking two integer variables andreturning an integer, but it is defined as a forward definition (prototype), and thus nobody is written. Later, we see that Add is defined with a body. Note that the twodefinitions of Add must be congruent with each other, or the compiler will complain.

    From the above example, we can also gather that in Pascal, a function's return valueis given by the value of the variable with the function's name (or by the variablenamed result ), when the function returns. As you can see in the Bad function, anundefined variable named "Bad" has been assigned a value. That is the return valuefor the Bad function. Similarly, in Add, the variable named "Add" has been assigneda value, which is its return value.

    Note that unlike C or other languages, assigning a return value to a function doesnot return from the function. Thus, the function will continue executing, as in thefollowing example:

    F unction Weird(A : Integer) : Integer;

  • 8/9/2019 Syntax and Functions Edit This Page

    4/36

    Var

    S : Integer;

    BeginS := A/2;

    If S < 10 ThenWeird := 1;

    S := S + 9;

    If S >= 10 ThenWeird := 0;

    Weird := 2;

    End;

    If A happens to be 6, the function will not return the expected result of 1 or even 0.Instead, it would return a result of 2, because the function to execute continues evenafter the return value is set. In fact, as you would notice, the function would return 2all the time because it runs all the way to the end, at which the return value is set to2.

    To mimic C style function returns, the exit statement must be used. The exitstatement in Pascal, unlike C, exits from the current block of code (in this case, thefunction), and NOT from the program. The code would then look like this:

    F unction Weird(A : Integer) : Integer;

    VarS : Integer;

    BeginS := A/2;

    If S < 10 ThenBeginWeird := 1;Exit;

    End;

    S := S + 9;

    If S >= 10 Then

  • 8/9/2019 Syntax and Functions Edit This Page

    5/36

    BeginWeird := 0;Exit;

    End;

    Weird := 2;End;

    Note that a third exit is not necessary at the end of the function since nothing elsewould be executed that could overwrite the function return.

    Category : Getting Started Add category

    Improve Borland Pascal Wiki by editing this page

    PetruD made an edit on June 2, 2008

    History

    Related changes

  • 8/9/2019 Syntax and Functions Edit This Page

    6/36

    v

  • 8/9/2019 Syntax and Functions Edit This Page

    7/36

  • 8/9/2019 Syntax and Functions Edit This Page

    8/36

  • 8/9/2019 Syntax and Functions Edit This Page

    9/36

  • 8/9/2019 Syntax and Functions Edit This Page

    10/36

  • 8/9/2019 Syntax and Functions Edit This Page

    11/36

  • 8/9/2019 Syntax and Functions Edit This Page

    12/36

  • 8/9/2019 Syntax and Functions Edit This Page

    13/36

  • 8/9/2019 Syntax and Functions Edit This Page

    14/36

  • 8/9/2019 Syntax and Functions Edit This Page

    15/36

  • 8/9/2019 Syntax and Functions Edit This Page

    16/36

  • 8/9/2019 Syntax and Functions Edit This Page

    17/36

  • 8/9/2019 Syntax and Functions Edit This Page

    18/36

  • 8/9/2019 Syntax and Functions Edit This Page

    19/36

  • 8/9/2019 Syntax and Functions Edit This Page

    20/36

  • 8/9/2019 Syntax and Functions Edit This Page

    21/36

  • 8/9/2019 Syntax and Functions Edit This Page

    22/36

  • 8/9/2019 Syntax and Functions Edit This Page

    23/36

  • 8/9/2019 Syntax and Functions Edit This Page

    24/36

  • 8/9/2019 Syntax and Functions Edit This Page

    25/36

  • 8/9/2019 Syntax and Functions Edit This Page

    26/36

  • 8/9/2019 Syntax and Functions Edit This Page

    27/36

  • 8/9/2019 Syntax and Functions Edit This Page

    28/36

  • 8/9/2019 Syntax and Functions Edit This Page

    29/36

  • 8/9/2019 Syntax and Functions Edit This Page

    30/36

  • 8/9/2019 Syntax and Functions Edit This Page

    31/36

  • 8/9/2019 Syntax and Functions Edit This Page

    32/36

  • 8/9/2019 Syntax and Functions Edit This Page

    33/36

  • 8/9/2019 Syntax and Functions Edit This Page

    34/36

  • 8/9/2019 Syntax and Functions Edit This Page

    35/36

  • 8/9/2019 Syntax and Functions Edit This Page

    36/36