Computer Science Canada

Returning to a Procedure

Author:  Anonymous [ Thu May 25, 2006 6:04 pm ]
Post subject:  Returning to a Procedure

Is it possible to say, have this:

code:

proc MAP_1
if (x = true) then
MAP_2
end MAP_1

proc MAP_2
if (x = false) then
MAP_1
end MAP_2


I really need this to help me in my RPG to make it so when I touch the town I enter it, then when I touch the gate to the outside, I come back out.

Author:  TheOneTrueGod [ Thu May 25, 2006 6:17 pm ]
Post subject: 

This is the circular dependance problem. No, in a language like turing, It is not possible to have that directly. HOWEVER, there are ways to simulate things like this. Read Cervantes' topic in the tutorials about turing as a functional language. You can simulate a procedure using a function. There are a couple other options, depending on what you need done.

#1) You could use recursion,
code:

procedure MAP_1 (x : int)
   if x > 0 then
      MAP_1 (x - 1)
   end if
end MAP_1


#2) You could have a variable that returns from the procedure, and tells you if you need to call the second procedure or not.

#3) Use a different programming language Razz (One that is compiled not interpreted [I believe I said that right... someone correct me if not])

Author:  MysticVegeta [ Thu May 25, 2006 6:39 pm ]
Post subject:  Re: Returning to a Procedure

vahnx wrote:
Is it possible to say, have this:

code:

proc MAP_1
if (x = true) then
MAP_2
end MAP_1

proc MAP_2
if (x = false) then
MAP_1
end MAP_2


I really need this to help me in my RPG to make it so when I touch the town I enter it, then when I touch the gate to the outside, I come back out.


well you need an exit condition on one of the proc.. so that it doesnt get stuck in a circle...

Author:  TheOneTrueGod [ Thu May 25, 2006 6:45 pm ]
Post subject: 

lol, mystic vegeta, its not possible. It'll give you the error "MAP_2" has not been declared. Turing works linearly. Ready my previous post, and try it out if you don't believe me Razz

Author:  MysticVegeta [ Thu May 25, 2006 6:51 pm ]
Post subject: 

I thought you were familiar with forward and body procs...

code:
var x : boolean
forward proc MAP_2

proc MAP_1
    if (x = true) then
        MAP_2
    end if
end MAP_1

body proc MAP_2
    if (x = false) then
        MAP_1
    end if
end MAP_2

Author:  TheOneTrueGod [ Thu May 25, 2006 7:41 pm ]
Post subject: 

lol, nope. I've used em in classes, but only to have multiple copies of the same procedure Razz. (Neither was vahnx. You gotta explain that when helping Very Happy)

Author:  Anonymous [ Thu May 25, 2006 8:40 pm ]
Post subject: 

Thanks so much MysticVegeta! I've been wondering this since i started Turing like 4/5ths a semester ago. You truly are a programming god. Did I ever tell you that.... I...... Love you.... (*kiss*)

Also, is there a way to also transfer a variable from inside that body procedure?

Author:  TheOneTrueGod [ Thu May 25, 2006 9:03 pm ]
Post subject: 

either global variables or

forward proc MAP_2 (var x : int, y : boolean /*etc*/)

Author:  MysticVegeta [ Fri May 26, 2006 2:12 pm ]
Post subject: 

vahnx wrote:
Also, is there a way to also transfer a variable from inside that body procedure?
Do you mean transfer a variable from inside to the outside or from outside to the inside..

Cause if its from outside to inside then truegod's post will help you else if its from inside to outside then you need "functions" that return a value like..
code:
fcn square (x : int) : int
      return x**2
end square

put square (2) % Outputs 4

var squaredNumber := square (someNumber) % Transfers the value from inside the function to the outside by storing the returning value in "squaredNumber" variable.

Author:  Anonymous [ Sat May 27, 2006 8:16 am ]
Post subject: 

Yeah for my game I just made it a global variable, so it doesnt really matter now. And I could use () probably.


: