Syntax and functions

GS undefined

Syntax
As you've seen in the earlier chapter, Pascal programs have a standard structure which looks like the following:

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

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

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

A declaration in the  block יas the following syntax:

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

Basic numeric types include:  (32-bit, hardware dependent),   (16-bit, hardware dependent),   (8-bit). Their unsigned counterparts are cardinal (available only in some versions of Pascal),,. Decimal numbers are supported using the  type, and the   type (available only in some versions of Pascal)

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

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

Functions/Procedures
Before we begin, let us first clarify the key difference between functions and procedures. A procedure is set of instructions to be executed, with no return value. A function is a procedure with a return value. For readers familiar with C/C++, a procedure is simply a function with a void return value, as in void proc_sayhello.

The definition of function/procedures is thus as such:

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

In this example, Add is first defined as a function taking two integer variables and returning an integer, but it is defined as a forward definition (prototype), and thus no body is written. Later, we see that Add is defined with a body. Note that the two definitions 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 value is given by the value of the variable with the function's name (or by the variable named ), when the function returns. As you can see in the Bad function, an undefined variable named "Bad" has been assigned a value. That is the return value for the Bad function. Similarly, in Add, the variable named "Add" has been assigned a value, which is its return value.

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

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 even after the return value is set. In fact, as you would notice, the function would return 2 all the time because it runs all the way to the end, at which the return value is set to 2.

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

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