--- In [email protected], davec <[EMAIL PROTECTED]> wrote:
>
> Hi,
> First post...
> 
> What is the advantage of passing parameters into and
> out of a subroutine? Why not declare and use global
> variables -- no passing at all.

Dangerous: just consider two distinct functions trying to change the
same global variable, and you're into deep trouble.

> I've written a program that calls some subroutines, and
> it occurred to me that simply declaring globals and not
> passing parameters is simplest.

Easy to do: yes.
Easy to understand after you haven't worked on the code for months: no.
Easy to maintain in terms of side effects: impossible.

> I Googled some, but with such broad terminology
> (subroutines, global, parameters) the flood of hits
> wasn't very helpful.

I wish I would have had a forum like this when I was a newbie, sigh...

> What are the pro's and con's of simply declaring all
> variables used in the subroutine as global and be
> done with it?

It's easy to write in the first place, that's true. And when writing a
huge Turbo Pascal application for MS-DOS (some 120 source code
modules), it definitely was easier to do this way, and it was noteably
faster and less memory consuming than having local variables.

However, maintaining such code is a nightmare. Consider you need some
array of database records you have to read and search through. Can you
use a variable named "DBIndex"? Probably such a variable already
exists. So you have to look for another name which is not yet in use.
You end up having hundreds of global variables for exactly the same
purpose, namely for indexing an array or so. Not good to survey.

I was a seasoned Pascal programmer at these times, and I needed four
weeks plus some time from the original programmer to understand this
Turbo Pascal program, how it worked in general, and how many of the
details worked. I have written some 700 KB of pure text for
documentation, and no one (I repeat it: NO ONE) ever dared to cope
with this program after I've left the company.

There are tons of more examples why this is generally considered bad
practice. I'm just too lazy to look them up now. ;-)
Just trust me, in general it's not worth the hassle. There are cases
where global variables really make sense, but these are (almost) all
special cases.

Regards,
Nico

Reply via email to