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

Username:   Password: 
 RegisterRegister   
 Procedure Problems
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ProgrammingFun




PostPosted: 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)

  Bye

end Hello

procedure Bye
put "bye"
delay (1000)

Hello

end Bye

% 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
Sponsor
sponsor
TerranceN




PostPosted: 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:
forward proc Bye() % declared before implementation, proc is a short form for procedure

proc Hello()
    put "Hello"
    delay(1000)
   
    Bye()
end Hello

body proc Bye() % implemetation of previously declared function
    put "Bye"
    delay(1000)

    Hello()
end Bye

Hello()


Hope that helps.
ProgrammingFun




PostPosted: Fri Jan 15, 2010 8:37 pm   Post subject: Re: RE:Procedure Problems

TerranceN @ Fri Jan 15, 2010 8:33 pm wrote:
Yes you can! You can use the forward and body keywords, here is an example using similar code to yours:

Turing:
forward proc Bye() % declared before implementation, proc is a short form for procedure

proc Hello()
    put "Hello"
    delay(1000)
   
    Bye()
end Hello

body proc 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:
forward proc procedure ()
before the code and then add () wherever the procedure is declared?
TerranceN




PostPosted: 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:
forward procedure SayHello


You tell turing what the function does by using the body keyword. This can be anywhere in the same file.

Turing:
body procedure SayHello
    put "Hello"
end SayHello


Therefore you can call it before you tell turing what it does.


Turing:
forward procedure SayHello

SayHello

body procedure 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:
forward function Add(num1 : int, num2 : int) : int

put Add(5, 7)

body function Add(num1 : int, num2 : int) : int
    result num1 + num2
end Add
ProgrammingFun




PostPosted: 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 Laughing ):

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




PostPosted: 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.

So no, there is no difference when you export.

Turing:
module Functions
    export A
   
    forward procedure B
   
    procedure A
        put "a"
        delay(1000)
       
        B
    end A
   
   
    body procedure B
        put "b"
        delay(1000)
       
        A
    end B
   
end Functions

Functions.A


Hope this helps.
ProgrammingFun




PostPosted: Sat Jan 16, 2010 9:07 am   Post subject: RE:Procedure Problems

Yes, I tried it and it works. Thank you for your help.
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  [ 7 Posts ]
Jump to:   


Style:  
Search: