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

Username:   Password: 
 RegisterRegister   
 Problem with declaring procedures
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TipsyCzar




PostPosted: Tue Nov 22, 2016 8:54 am   Post subject: Problem with declaring procedures

What is it you are trying to achieve?
Trying to make procedures that call upon each other (Start at a menu, go to a game, back to the menu)


What is the problem you are having?
"'procedure' has not been declared' is the error given

Describe what you have tried to solve this problem
tried re-arranging the order of procedures (obviously didn't work)


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
(This is just a program I created in a few minutes)

Turing:


var answer:string
var number:int
var guess:int
var x,y,b:int

proc menu
put "Welcome to the main menu. Would you like to play the game?"
get answer
if answer = "yes" then
cls
game
end if
end menu

proc game
put "Click the green box to win. To go back to the menu, click the red box"
drawfillbox (maxx - 25, 0, maxx, 25, red)
drawfillbox (10, 30, 50, 90, green)
loop
mousewhere (x,y,b)
if x <= maxx and x >= maxx - 25 and y <= 25 and b = 1 then
cls
menu
elsif x <= 50 and x >= 10 and y <= 90 and y >= 30 and b = 1 then
put "You clicked the box!"
end if
end loop
end game

%calls upon the menu
menu




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




PostPosted: Tue Nov 22, 2016 9:32 am   Post subject: RE:Problem with declaring procedures

You're about to learn a hard lesson about the stack.

You can make your code work as-is using forward declarations (look up the 'forward' keyword in the Turing documentation). But it's wrong. It will work. It will probably pass all of your bug tests because they won't test for this. What's gonna happen, if you keep this running for a while and go between the main menu and the game lots of times, is the program is gonna crash. Here's why:

The computer has what we call a stack that controls what code gets run next (sort of). When you call a function, it gets added to the stack. When that function finishes, the stack deletes all variables associated with that function, goes back to the code that called it and keeps on running. However, the computer can only remember so many functions at a time, and if you load too many functions at once, the program will crash with a 'stack overflow' error.

For example, in your code, we've got functions game and menu. The first thing that happens in the program is we call menu, so your computer loads all of the variables inside menu into RAM. As the menu code is executed, we get to the line that calls game. So menu gets paused, and game gets loaded into memory and added to the stack. Now as we execute game, we find a line that calls menu again. So menu gets loaded into RAM again, game gets paused, and we start executing menu. But we already had a menu loaded in RAM, so now there's two (the first menu never finished, it only paused, remember?). And inside menu we call game again, so another game gets loaded into memory. Now our stack is four functions deep: menu -> game -> menu -> game. Game will call menu again, which will call game again, and this will continue until we have too many functions loaded and we get the stack overflow crash.

The way to solve this is to let functions finish. Remember, when a function ends, the stack goes back to the function it paused and resumes. If your game function ends before calling menu, then the stack will return to the previous menu and resume it.

So how does all this apply to you? Instead of calling 'menu' when you click the red box, just end the function. It will go back to the menu.

But you'll have to change your menu function as well, because right now, as soon as it resumes, it will run out of code and end. You don't call menu from game anymore, so we need another way to get back to the start of the function. How do we generally go back to code we've already run? With a loop of course!

Now that our program won't crash from stack overflows, it can keep running until the universe ends or the hardware fails!
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: