Newsgroups: comp.lang.c Path: utzoo!henry From: henry@utzoo.uucp (Henry Spencer) Subject: Re: An Ethics Question (global variables) Message-ID: <1989Apr7.190827.4289@utzoo.uucp> Organization: U of Toronto Zoology References: <1819@uop.edu> Date: Fri, 7 Apr 89 19:08:27 GMT In article <1819@uop.edu> jeff@uop.edu (Jeff Ferguson) writes: >Global variables or passed parameters? I'm anxiously awaiting the pros >and cons of this issue. Thank you. Parameters are generally better than global variables. The more restricted scope gives less opportunity for other parts of the program to modify them in surprising ways, and the explicit presence in the parameter list makes it easier to remember who uses what. Efficiency considerations can go either way, depending on the machine, although one significant issue is it's generally harder for compilers to know when it's safe and desirable to put global variables in registers. On almost any machine, the code will run a good deal faster if heavily- used variables are in registers. The down side is that it can be a royal pain passing parameters down through multiple layers of functions just because they're needed at the lowest level. This is especially true if they're used for things like error reporting that are peripheral to the function's intended purpose; then the clutter in the argument list obscures the real meaning. For this sort of thing, global variables are probably better. Almost anything that relies on global variables is problematic in the presence of multiple processes within the same address space (aka threads). -- Welcome to Mars! Your | Henry Spencer at U of Toronto Zoology passport and visa, comrade? | uunet!attcan!utzoo!henry henry@zoo.toronto.edu