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

Username:   Password: 
 RegisterRegister   
 Returning multiple values from function
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jQwotos




PostPosted: Wed Apr 05, 2017 12:56 pm   Post subject: Returning multiple values from function

What is it you are trying to achieve?
Is there a way to return multiple value from a function?

What is the problem you are having?
Trying to return multiple values from a function
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
For example, in python you can return multiple values from a function, is that possible in turing?
Python:

return 1, 2, 3

Please specify what version of Turing you are using
latest
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed Apr 05, 2017 8:22 pm   Post subject: RE:Returning multiple values from function

There's a number of ways you can accomplish this. You could bundle the values into a record, or you could pass variables by reference and edit them inside the function. Passing by reference is very subtle in Turing:

code:
proc PassByValue (a : int) % This function passes by value
     a := a + 1 %this causes an error.
end PassByValue

proc PassByReference (var a : int) %Notice we added the keyword 'var' this time
    a := a + 1 %this works just fine!
end PassByReference

var foo := 5
PassByReference (foo)

put foo %output is 6


I actually didn't know about this small difference until I looked it up to write this down. That's a pretty neat little feature! Unfortunately, this method won't let you do anything fancy. You can't pass a procedure as an argument to another function, for example, since the procedure doesn't really return anything. It may just cause more problems than it solves.


The Record solution doesn't have these drawbacks, but it does bundle your values into a single variable (which is how you can return it). This may or may not be a problem depending on what you're trying to do. If it's something like x/y coordinates, bundling them up makes sense (and may streamline a whole lot of your code). Having said that, I have never been in a situation where I wanted to return multiple unrelated variables and I'm not sure why you would! So, basically, you should be using records (or other types that Turing supports).
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  [ 2 Posts ]
Jump to:   


Style:  
Search: