Local Variables (New Features)

by M Santos, Monday, October 29, 2012, 19:18 (1817 days ago)

Hi all
I don´t know if this is possible in basic as variables are not declared, but I´m doing a program with lots of subroutines and it would be handy to use the same letter with efect only inside each subroutine.
Regards
Manuel

Local Variables

by Jim ⌂ @, Russell, KY, Monday, October 29, 2012, 21:56 (1816 days ago) @ M Santos

Manuel,

Variables INSIDE functions and subroutines are local (except the function name inside a function). This was how they were introduced in 0.9.9.x.

a = 10
b = 99
print "in main program"
print "a = " + a
print "b = " + b
call testsub(a)
print "back in main program"
print "a = " + a
print "b = " + b
end

subroutine testsub(a)
print "in testsub"
a = a * 2
b = 77
print "a = " + a
print "b = " + b
end subroutine

Is this what you were asking for?

JIm

Local Variables

by M Santos, Tuesday, October 30, 2012, 21:10 (1815 days ago) @ Jim

Hi Jim
I´m doing a project with my year 7 class. Each student makes a car or a house and then I put it in a subroutine and add some movement.
See the example below.
We have to use a different variable x x1 x2 x3 .... for each car, or else the program will consider as a unique variable.
Regards
Manuel

graphsize 900,500
fastgraphics

for p = 1 to 100000 step 1
call ritacar(p)
call sidneycar(p)
refresh
clg
next p


subroutine ritacar(p)
if p=1 then x=400
x=x+1
if x>1000 then x = 0
y=60
color yellow
rect 910-x,270+y,60,20
color yellow
rect 930-x,230+y,70,60
color black
font "Arial Black",12,50
Text 935-x,230+y,"School"
color darkcyan
rect 935-x,250+y,60,20
color black
circle 925-x,290+y,10
color black
circle 990-x,290+y,10
end subroutine

subroutine sidneycar(p)
if p=1 then x1=0
x1=x1+1.5
if x1>1200 then x1 = 0
color darkblue
rect 910-x1,270+y,60,20
color darkblue
rect 930-x1,260+y,40,20
color black
circle 925-x1,290+y,10
color black
circle 955-x1,290+y,10
end subroutine

Local Variables

by Jim ⌂ @, Russell, KY, Tuesday, October 30, 2012, 22:31 (1815 days ago) @ M Santos

Variables are being stored based on the call stack depth and are static between calls. This behavior can get crazy at times and we need to look at it, it was the easiest way to implement local variables.

The following code does not use the static nature that you were having problems with.

Let me think about this.


graphsize 900,500
fastgraphics

ritax=400
sidneyx = 400

for p = 1 to 100000 step 1
call ritacar(ritax,60)
call sidneycar(sidneyx,60)
ritax = ritax + 1
if ritax>1000 then ritax = 0
sidneyx = sidneyx + 1.5
if sidneyx>1200 then sidneyx = 0
refresh
clg
next p


subroutine ritacar(x,y)
color yellow
rect 910-x,270+y,60,20
color yellow
rect 930-x,230+y,70,60
color black
font "Arial Black",12,50
Text 935-x,230+y,"School"
color darkcyan
rect 935-x,250+y,60,20
color black
circle 925-x,290+y,10
color black
circle 990-x,290+y,10
end subroutine

subroutine sidneycar(x,y)
color darkblue
rect 910-x,270+y,60,20
color darkblue
rect 930-x,260+y,40,20
color black
circle 925-x,290+y,10
color black
circle 955-x,290+y,10
end subroutine

Local Variables

by M Santos, Wednesday, October 31, 2012, 16:00 (1815 days ago) @ Jim

Thanks.
That looks much more orthodox (read professional).
A good example on "Basic let's you do all the wrong things".
Regards
Manuel

Local Variables

by Jim ⌂ @, Russell, KY, Wednesday, October 31, 2012, 18:27 (1815 days ago) @ M Santos

In the new version (on SVN only 0.9.9.12) garbage collect has been added when a function/subroutine returns the local variables (within that call) are no longer kept, as in most other languages.

You highlighted a problem I hadn't thought of with the code you originally submitted.

Thanks. :)

RSS Feed of thread
powered by my little forum