Posted: Fri Jan 15, 2010 8:05 pm Post subject: Procedure Problems
What is it you are trying to achieve?
I am trying to call a procedure which uses another procedure which is declared later on.
What is the problem you are having?
I am making a program which has a lot of buttons. Sometimes, one of my buttons on a certain page calls to a procedure which is declared after the current procedure which I am using. Is there any way to get around this problem without having to create duplicates of all existing procedures?
What have you tried to solve this problem?
I have tried importing the procedures but this produces the same error.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing:
% an example
proceudre Hello
put"Hello" delay(1000)
% when this program is run, it will say that Bye has not yet been declared % can I get around this problem without having to declare a duplicate of Bye before the Hello as well?
%thanks for your help and time
Please specify what version of Turing you are using
4.1.1
Sponsor Sponsor
TerranceN
Posted: Fri Jan 15, 2010 8:33 pm Post subject: RE:Procedure Problems
Yes you can! You can use the forward and body keywords, here is an example using similar code to yours:
Turing:
forwardproc Bye()% declared before implementation, proc is a short form for procedure
bodyproc Bye()% implemetation of previously declared function put"Bye" delay(1000)
Hello() end Bye
Hello()
Hope that helps.
Thank you so much! So I would declare
Turing:
forwardprocprocedure()
before the code and then add () wherever the procedure is declared?
TerranceN
Posted: Fri Jan 15, 2010 8:45 pm Post subject: RE:Procedure Problems
No, the important part is not the brackets (they are optional on procedures, but I use them because they are mandatory in other languages so I am used to it). The important part is forward and body
You declare a 'prototype' of the function (or procedure, but when I say function I mean both) with the forward keyword. A prototype just tells turing that a function like this exists, and how you are supposed to call it.
For example:
Turing:
forwardprocedure SayHello
You tell turing what the function does by using the body keyword. This can be anywhere in the same file.
Turing:
bodyprocedure SayHello
put"Hello" end SayHello
Therefore you can call it before you tell turing what it does.
Turing:
forwardprocedure SayHello
SayHello
bodyprocedure SayHello
put"Hello" end SayHello
I hope that makes it more clear.
EDIT: Also note that if you use a function with prameters, you have to include them in both the forward and body
ex
Turing:
forwardfunction Add(num1 :int, num2 :int):int
put Add(5, 7)
bodyfunction Add(num1 :int, num2 :int):int result num1 + num2
end Add
ProgrammingFun
Posted: Fri Jan 15, 2010 8:51 pm Post subject: Re: Procedure Problems
Yes, this made things a lot easier for me.
Just one more question (for now ):
will this also work in .TU files and when they are imported
and if so, will this change the way in which I export procedures from a .TU file?
Thank you very much.
TerranceN
Posted: Fri Jan 15, 2010 9:30 pm Post subject: RE:Procedure Problems
If it works in a module, it shouldn't matter what file it is in (unless there in an importing error).
I tried this in a module and it worked. I don't know about classes but I'm pretty sure it is the same.