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

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




PostPosted: Mon May 16, 2011 3:01 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon May 16, 2011 3:06 pm   Post subject: 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)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
user23




PostPosted: Mon May 16, 2011 3:35 pm   Post subject: 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




PostPosted: Mon May 16, 2011 4:39 pm   Post subject: 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.
Turing:
fcn countdown(x : int) : int
    put "x = ", x
    if x <= 0 then   % This is the base case
        result 0       % We must not call countdown here
    else
        result countdown(x - 1)
    end if
end countdown

var c := countdown(10)
put "countdown(10) = ", c



Tony is saying that your error is probably caused by doing something like
code:
if something then
    item(var1, var2)
end if

instead of
code:
if something then
    somevar := item(var1, var2)
    % OR
    result item(var1, var2)
end if

user23




PostPosted: Mon May 16, 2011 4:48 pm   Post subject: Re: Help with functions

Yes, I am trying to accomplish recursion.
So if I have something setup, where item (x,y) where x and y are user inputted values that have their own values from 0 - 8 that are calculated before hand based on the surroundings.
Then, I want to do

if total = 0 then
item (x*100,y*100) % this runs the function from the start with the new multiplied values
item (x+1, y+1) % this runs the function from the start with the sums.
..
..
elsif
..
..
end if

The dots represent something similar to the item lines above. Would I need a new variable for each line (I need the function to be run for up to 8 different x, y values at a time)?

Thanks
chrisbrown




PostPosted: Mon May 16, 2011 4:59 pm   Post subject: Re: Help with functions

user23 @ Mon May 16, 2011 4:48 pm wrote:
Would I need a new variable for each line (I need the function to be run for up to 8 different x, y values at a time)?

That depends on what happens after the "end if." Some more context would help. What do x and y represent? Why are you both multiplying and incrementing them? Post more code if you have it.
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  [ 6 Posts ]
Jump to:   


Style:  
Search: