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

Username:   Password: 
 RegisterRegister   
 Procedures calling each other. Need help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
5c0r910n




PostPosted: Wed Jan 11, 2017 8:55 am   Post subject: Procedures calling each other. Need help

What is it you are trying to achieve?
For a while I've been trying to make a Arena game in Turing. For this certain part of the code I am making a store where players can buy healthkits and whatnot.


What is the problem you are having?
But the problem is the medstore is split into procedure to help the menu function. So procedure one will call procedure 2, and procedure 2 calls procedure 1. But procedure 2 comes before procedure 1, so neither will work




Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


proc medchoice1
if med1choice=1 then
if money>14 then
money:=money-15
hp:=hp+(hpcap*0.25)
if hp>hpcap then
hp:=hpcap
end if
else
put"\"Oops!You don't have enough money for that my friend!\""
end if
end if


if med1choice=3 then
if money>24 then
money:=money-25
hp:=hp+(hpcap*0.5)
if hp>hpcap then
hp:=hpcap
end if
else
put"\"Oops!You don't have enough money for that my friend!\""
end if
end if

if med1choice=3 then
if money>34 then
money:=money-35
hp:=hp+(hpcap*0.75)
if hp>hpcap then
hp:=hpcap
end if
else
put"\"Oops!You don't have enough money for that my friend!\""
end if
end if


proc med2choice2
if med2choice=1 then
if money>(hpboosterprice-1) then
money:=money-hpboosterprice
put"\"Okay,here you go!\""
else
put"\"You can't afford that!Nice try though.\""
end if
end if


if med2choice=2 then
if money>(dmgboosterprice-1) then
money:=money-dmgboosterprice
put"\"Okay,here you go!\""
else
put"\"You can't afford that!Nice try though.\""
end if
end if
medstore
end med2choice2


if med1choice=0 then
medstore
end if
end med1choice1
proc medstore

put"An old man with a cane in hand hobbles over to you"
put"\"Welcome! I assume you are here to buy some basic medical supplies right?\""
put"\"You can head to aisle 1 for the quick patch-me-ups that will get you in a better state of health right away, or aisle 2 for some long term boosters\""
put"(1 = Healing Items)"
put"(2 = Stat Items)"
put"(0 = Go Back)"
get medchoice"*
cls
if medchoice=1 then
put"
\"Aisle one it is!\""
put"\"Here we have the Original Medkit, for 15$ it patches up to 25% of your total health!\""
put"\"Here we have the Improved Medkit, for 25$ it patches up to 50% of your total health!\""
put"\"And finally we have the UBER Medkit, for 35$ it patches up to 75% of your total health!\""
put""
put"(1 = Orignal Medkit)"
put"(2 = Improved Medkit)"
put"(3 = UBER Medkit)"
put"(0 = Previous Menu)"
end if
if medchoice=2 then
put "\"Alright! Onwards, to aisle 2! \""
put "\"Here we have a health booster, because of the ever-waning supply of this item, the price will increase as more are bought\""
put"\"For you, the current price is \"", hpboosterprice

put "\"Here we have a damage booster, because of the ever-waning supply of this item, the price will increase as more are bought\""
put"\"For you, the current price is \"", dmgboosterprice
get med2choice:*
med2choice1
end medstore




Please specify what version of Turing you are using
I am using turing 4.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed Jan 11, 2017 10:37 am   Post subject: RE:Procedures calling each other. Need help

The quick and dirty answer is the 'forward' keyword, but that is technically wrong and might cause you some stability issues.

Every time you call a function it gets loaded into memory, and it stays there until it finishes execution. If that function, A, calls another function, B, then (generally), A can't finish until B is finished. If B then calls A, then we now have TWO A's in memory and one B. A2 has to finish before B can finish, then B has to finish before A1 can finish. Then A2 calls B, so now there's two B's and two A's. This will continue until you run out of memory and crash, or something finishes execution and everything gets cleaned up. It's unlikely that you'll ever run this program long enough to crash due to this, but your future programs might call many nested functions very quickly and those can crash immediately. So 'forward' will work for now, but it's just gonna cause you problems later on and it's better to just do it right from the start.

Remember, when a function finishes, it gives back control to the code that called it. If A called B, then when B finishes, we continue A where we left off. So we don't have to call A from B. Just let B finish. If there's a 'back to main menu' button, don't have it call 'a', just have it end the function, either with the 'result' keyword or by exiting all the loops and 'letting it run out of code'. Once you've practised this a few times it'll be second nature.
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: