[Gambas-user] Restrictive variable declaration

Benoit Minisini gambas at ...1...
Tue May 4 17:23:06 CEST 2004


On Monday 03 May 2004 23:00, Grahame White wrote:
> 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
>
>

You must understand that Gambas code is compiled before being interpreted.

When a function is executed, all local variables are reserved on the stack in 
one shot. Reserving local variables only when needed will take more time to 
execute for no memory gain, as the stack is a small memory allocation 
reserved at gambas startup (for example, 16K is sufficient for the IDE).

What you want is technically possible, but would need changes in the internal 
Gambas compiled code, so I can't do that before a long time!

And I find having local variables declared at the beginning of the function 
clearer B-)

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the User mailing list