Computer Science Canada Functions and Updating two values |
Author: | EiCCA [ Wed Jun 14, 2006 11:43 am ] | ||
Post subject: | Functions and Updating two values | ||
There are two questions I have here in this topic 1) In C++, you can declare a function type as void, meaning it does not return a value. What is the term for this in Turing? 2) I have a situation where I need two variables in a funtion to update their values after the function closes. For example:
In C++, you can do this using refrence paramaters, using the & beside the paramater name, however I do not know how this is done in Turing. Help? ![]() |
Author: | wtd [ Wed Jun 14, 2006 12:54 pm ] | ||
Post subject: | |||
1. Such things are known as procedures in Turing. 2. You can do this in Turing by prefixing the parameter name with "var ". However, for such a simple case, there is a better approach. Simply link both variables into a single data type, then return that.
Or in C++ simply use the std::pair class as defined by the STL. |
Author: | Tony [ Wed Jun 14, 2006 6:47 pm ] | ||
Post subject: | |||
the above clearly doesn't work. X and Y are local variables. What'd you'd need to do is pass the reference to the variable's address. Now you're getting into pointers and what not. Turing might actually have a similar syntax involving &, but I don't recall at the moment. Consult your F10 manual. Raguardless, the best practice is to have the function return the updated values. |
Author: | [Gandalf] [ Wed Jun 14, 2006 7:01 pm ] | ||
Post subject: | |||
Tony, like wtd said you can get the pass by reference effect by prefixing the parameter name with "var", for example:
Though yes, you could also return a type containing all your data. |
Author: | Tony [ Wed Jun 14, 2006 7:03 pm ] |
Post subject: | |
ah. thx for pointing that out. |