
-----------------------------------
user23
Mon May 16, 2011 3:01 pm

Help with functions
-----------------------------------
So, I have a function set that is defined as function item (x : int, y : int) : int
I want to be able to modify x and y, and for example call item (x*100,y*100) within an if statement, but I get the error "expression is not a procedure and hence cannot be called"
Anyone know how to make this work?

Thanks

-----------------------------------
Tony
Mon May 16, 2011 3:06 pm

RE:Help with functions
-----------------------------------
since a function returns a value, Turing expects you to "receive" that value. E.g.
[code]
var foo : int
foo := item(x,y)
[/code]

-----------------------------------
user23
Mon May 16, 2011 3:35 pm

Re: Help with functions
-----------------------------------
Hmm I think I didn't explain myself well enough.  What I'm trying to do is re-run the function from the beginning basically with the different values that are calculated (from for example y*100, x*10 - where the original numbers are multiplied and put back through the function.

Thanks

-----------------------------------
chrisbrown
Mon May 16, 2011 4:39 pm

Re: Help with functions
-----------------------------------
Sounds like recursion.

You can certainly call a function from within itself. You will have to have what's called a base case, which is just an if statement that ensures the function eventually ends without calling itself, otherwise it will run out of memory (by what's called stack overflow).

Run this to see the basic idea.
fcn countdown(x : int) : int
    put "x = ", x
    if x 