Skip to content

Strange Types Circulating (in code)

Let's answer gradually.
First of all, it can be assigned not only to variables, but to constants, functions and procedures too.
Very quickly: variables and constants host values manageable inside and through functions or procedures.
The first are the "who or what", the last are the "how".
Finally... while variable are changeable by definition, constants aren't; on the other side... while functions return a value, procedures simply don't.

Some declaration examples:

Const
MAX_PLAYERS = 10; //Identifier MAX_PLAYERS with prefixed value 10

type
  ...
    //Procedure
    procedure ButtonStartClick(Sender: TObject);
  ...
  end; 

var  //Two variables of two different types
  FormMain: TFormMain;
  GlobalInteger : integer; //A Global Variable declaration

implementation

procedure DuplicateProcedure(v:integer);
var
  GlobalInteger : integer; //A Local Variable declaration
begin

end;

function DuplicateFunction(v:integer):integer; //response
var
 GlobalInteger : integer; //Another Local Variable declaration
begin
  result := ...;
end;

MAX_PLAYERS, ButtonStartClick, FormMain, GlobalInteger, DuplicateProcedure, DuplicateFunction are all unique identifiers.
Literally all three GlobalInteger are surely identifiers: what about their uniqueness?
They all indicate the same variable (that is the same memory location) or three completely different? or only two are the same?

Look at their respective var expression preceding them:

  1. the first is truly global: this means it's visible and manageable by any following procedure or function;
  2. the second is inside a procedure's body: only code inside this can manage it and nobody else;
  3. same as previous, just for a function;

and this means that any operations inside procedures or functions involving GlobalInteger are directed only to their local variable, without affecting the global one. We have three distinct variables.
Summarizing, if you want to manage a global variable then simply take care to not declare a local variable with the global's same name.
This is called scope of a variable, to indicate its degree of visibility by code portions.

When you want to emphasize a concept without affecting the code you can use comments; they are a powerful way to quickly explain a code's meaning.
We surely recommend to use them from the beginning and in abundance; by acquiring experience you'll discover how precious they could for a lot of reasons like sharing it with workmates or avoiding to spend time trying to understand an old code of yours you don't see since just few months.
We'll repeat to boredom: comments are vital!
In Pascal you have two modes:

  • // double backslashes, valid for single lines;
  • {
    braces parentheses to group more lines
    }

Now, look at the procedures and functions shown in code: you can see some arguments inside parentheses.

They're variable, again (and yes! they're strictly local); declared there so that compiler knows what the procedures and functions are going to receive from outside when called.
Who can call them? actually any other code block can, because their scope is... global!