Board index » delphi » Turn off global variables

Turn off global variables

I am a high school teacher working with TP 7.0.  I have noticed that
when variables are declared, they are by global by default, and don't
need to be passed to procedures and functions.  Is there a way to turn
this off so that all variables MUST be passed to procedures and
functions? I am trying to teach young people good programming habbits.

 

Re:Turn off global variables


sw...@ix.netcom.com(James S. Wash ) wrote:

Quote
>I am a high school teacher working with TP 7.0.  I have noticed that
>when variables are declared, they are by global by default, and don't
>need to be passed to procedures and functions.  Is there a way to turn
>this off so that all variables MUST be passed to procedures and
>functions? I am trying to teach young people good programming habbits.

When a variable is declared in the main block of a program, it is global and it
cannot be "turned off". But you can declare variables after declaring
functions, eg:

program example;

function a(....);
.. { function's body }

procedure b(....);
.. { procedure's body }

var not_so_global_var: any_type;

begin
.. { not_so_global_variable_var is visible only here }
end.

This way of declaring vars will make your students pass variables into
functions and procedures as parameters. Note this is not available in standard
Pascal.
Hope this helps.

--
  ___ _  _
 | . ) |/ /   Piotr Keplicz   kepl...@crab.mimuw.edu.pl
 |__||_|\_\   http://melkor.mimuw.edu.pl/~keplicz/piotr

Other Threads