[Gambas-user] Restrictive variable declaration

Grahame White grahame at ...307...
Mon May 3 23:00:37 CEST 2004


Is there a reason that variable have to be declared at the very beginning 
of a function or procedure?

For example in the following code :

public sub do_something (foo as integer)

	dim i as integer

	if foo > 100 then
		do_other_stuff()
	else
		for i = 1 to 42
			do_yet_more_stuff
		next
	endif

end sub



If foo is less than 100 then it'll use the variable 'i' which was declared 
at the beginning otherwise that 'i' is just wasteful overhead. Now if 
variables could be declared in with other code then 'i' could have been 
declared just after the else.

As long as the variable is declared before it is *used* I don't see why the 
current "only at the beginning of the function/procedure" method should 
be.


Grahame




More information about the User mailing list