Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do you use a variable declared in a procedure in another procedure
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
1kd7




PostPosted: Wed Oct 01, 2014 1:50 pm   Post subject: How do you use a variable declared in a procedure in another procedure

What is it you are trying to achieve?
Im trying to use a variable declared in a procedure in another procedure.


What is the problem you are having?
Its not recognizing it as a declared variable.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


procedure RollDie4
x:=x+125
var Roll4:int:=Rand.Int(1,6)
for i:1..5;
Die2;
delay(50);
Die4;
delay(50);
Die5;
delay(50);
Die1;
delay(50);
Die3;
delay(50);
Die5;
delay(50);
Die2;
delay(50);
Die6;
delay(50);
Die4;
delay(50);
Die3;
end for;

if Roll4 = 1 then
Die1
elsif Roll4 = 2 then
Die2
elsif Roll4 = 3 then
Die3
elsif Roll4 = 4 then
Die4
elsif Roll4 = 5 then
Die5
elsif Roll4 = 6 then
Die6
end if
end RollDie4

procedure RollDice
RollDie
RollDie2
RollDie3
RollDie4
if Roll=Roll2 or Roll2=Roll3 or Roll=Roll2 or Roll=Roll4 or Roll2=Roll4 or Roll3=Roll4 then
Draw.Text ("DOUBLES!!", 50, 300, font, red)
elsif Roll=Roll2 and Roll2=Roll3 or Roll=Roll2 and Roll2=Roll4 or Roll2=Roll3 and Roll3=Roll4 or Roll=Roll3 and Roll=Roll4  then
Draw.Text ("Triples!!!", 50, 300, font, red)
elsif Roll=Roll2 and Roll2=Roll3 and Roll3=Roll4 then
Draw.Text ("Quadruples!!!!", 50, 300, font, red)
else

end if
end RollDice



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Oct 01, 2014 2:09 pm   Post subject: RE:How do you use a variable declared in a procedure in another procedure

It's a matter of scope, and it's a good thing --in a multi-million lines projects (e.g. web browsers, operating systems, etc.) you wouldn't want your "count" variable to interfere with many other similarly named variables.

In your case, the variable can be declared outside of the procedure first to get a larger scope (bad design, one typically wants to limit the scope of variables), or have the procedure return the value, becoming a function.

I also suspect that your RollDie, RollDie2, RollDie3, and RollDie4 are just copy-pasted code, with some values changed around. You could combine them all into a single function, and use an argument for those minor changes.
code:

function roll_die(num : int) : string
   result "rolling die number: " + intstr(num)
end roll_die

put roll_die(1)
put roll_die(2)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: