Computer Science Canada Procedures calling each other. Need help |
Author: | 5c0r910n [ 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>
Please specify what version of Turing you are using I am using turing 4.1 |
Author: | Insectoid [ 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. |