need help with functions
Author |
Message |
jr.ranger.33
|
Posted: Thu Nov 02, 2006 10:19 am Post subject: need help with functions |
|
|
im just wondering if there is a way to declare an operation as a variable. for example:
var plusminus : string
plusminus:= "+"
so that i can use it by saying
put 5 plusminus 5
and it will display
10
Somebody told me that i can do this using the function command. So i need to know how to use a variable as an operation, and if it can be done by using the function command, how do i use this function command. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Clayton
|
Posted: Thu Nov 02, 2006 10:34 am Post subject: (No subject) |
|
|
see the Turing Walkthrough for the tutorial on functions, which is what you are looking for. |
|
|
|
|
|
richcash
|
Posted: Thu Nov 02, 2006 12:54 pm Post subject: (No subject) |
|
|
You can't really use it like the way you want to. You have to pass arguments or parameters to your function, which you do in brackets.
code: | function Add (num1, num2 : int) : int
result num1 + num2
end Add
put Add (5, 5) %will output 10 |
|
|
|
|
|
|
Clayton
|
Posted: Thu Nov 02, 2006 1:38 pm Post subject: (No subject) |
|
|
well you can do something like this:
Turing: |
fcn operation (op : string) : real
%do whatever based on op
end operation
|
that is... if i understand your problem correctly (this is why a functional language is very very handy.) |
|
|
|
|
|
richcash
|
Posted: Thu Nov 02, 2006 8:35 pm Post subject: (No subject) |
|
|
No, I meant you can't use it like how he wants to use it in his first post, in terms of you can't make new keywords or operators. You can never write this :
you have to make a function and use it accordingly :
code: | fcn times (n1, n2 : real) : real
result n1 * n2
end times
put times (3, 4) |
|
|
|
|
|
|
[Gandalf]
|
Posted: Fri Nov 03, 2006 4:33 am Post subject: (No subject) |
|
|
Ahh... This reminds me of a certain other language...
richcash, what if everything is an object? What if you don't explicitly need parenthesis or have to worry about other syntax? What if functions/procedures can be named using symbols (even operator symbols)?
Then, something like so:
code: | myInteger.assign (5)
put myInteger.add (2) % => 7 |
Can become:
code: | myInteger = 5
put myInteger + 2 % => 7 |
Or, more relevant to our situation:
This would then work, since 5 is an integer object, and you're calling the "+" function of that object, and passing 2 to it.
Sadly, this is not very possible in Turing because of it's clumsy object oriented syntax and features. Even getting a simple example of this becomes difficult:
code: | class Integer
export equals, times
var val : int
proc equals (i : int)
val := i
end equals
fcn times (i : int) : int
result val * i
end times
end Integer
var age : ^Integer
new Integer, age
age -> equals (5)
put age -> times (2) |
It's possible to improve this, even model Turing in a semi-completely-object-oriented way, but it's easier to just switch to another, better, language. |
|
|
|
|
|
richcash
|
Posted: Fri Nov 03, 2006 11:02 am Post subject: (No subject) |
|
|
Interesting [Gandalf].
But you still haven't created a new operator in Turing, it is impossible, and that's all I was trying to point out from the first post. Functions work great, but it's just you can't make them look as nice as the div operator, for example.
Using OOP like that, one of your "so-called" operands must be an object or at least a variable, which is not a requirement of operators. Plus, the '->' is just a short-form notation and is the minimum required. Plus, you're still passing a parameter in brackets. This technique is interesting, but it's still not declaring new Turing operators. |
|
|
|
|
|
Tony
|
Posted: Fri Nov 03, 2006 11:25 am Post subject: (No subject) |
|
|
As Gandalf points out
[Gandalf] wrote: What if you don't explicitly need parenthesis or have to worry about other syntax?
it's all just shortcuts and aliases.
It might be feasable to change some of Turing's configuration files to allow for such syntatical sugar. Ultimatly you'd be looking for Lexer to parse your keyword into a method call, and/or have a macro added to the C++ code before it compiles.
But if you get the functionality out of the object you're working with... I don't see why
should be treated differently from
one is an alias for another, it all compiles the same. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Sponsor Sponsor
|
|
|
richcash
|
Posted: Fri Nov 03, 2006 4:22 pm Post subject: (No subject) |
|
|
I didn't mean to start such a confusion, I was just saying that if jr.ranger wants to use a function, he can't use it like a keyword. div in Turing is not a function. Yes, he can make a function call look like a keyword (sort of) with little tricks, but I'm just making sure he knows the difference between a keyword and a function. |
|
|
|
|
|
|
|