Default values for function?
Author |
Message |
Jay
|
Posted: Tue Feb 22, 2005 10:58 am Post subject: Default values for function? |
|
|
Hey all, I've google'd and searched all this forum, and now I'm finally desperate and posting. Is there any way in Turing to have default values for a function's parameters? I can't find anything that points one way or the other, but I figure if I haven't found anything by now there's probably no way. I also wanted to use function overloading, but found a thread that confirms that that's not possible.
Thanks. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Tue Feb 22, 2005 1:42 pm Post subject: (No subject) |
|
|
there's a way to fake function overload in Turing. You'd have to use pointers to functions and when you want to overload something, just change the pointer to a new function. Should work.
As for default values : there're two approaches to the problem.
1) make all of arguments be string, and if NULL ("") is passed, you make it a default value instead. For integers and floats you can use strint() and strreal().
Turing: |
function foo (bar : string) : string
if bar not= "" then
result bar
else
result "default"
end if
end foo
put foo ("bar")
put foo ("")
|
2) Might be a more interesting approach: accepting an array of arguments. The size will be flexable, it is actually possible to incorporate multiple constructors for the same function.. (hardcoded overloading?)
Turing: |
function foo (bar : array 0 .. * of string) : string
var out : array 0 .. 4 of string := init ("1", "2", "3", "4", "5")
var answer : string := ""
if upper (bar ) not= 0 then %atleast one argument has been passed
for i : 0 .. upper (bar )
out (i ) := bar (i )
end for
end if
for i : 0 .. upper (out )
answer + = out (i ) + " :: "
end for
result answer
end foo
var myArguments1 : array 0 .. 0 of string := init ("")
put foo (myArguments1 )
var myArguments2 : array 0 .. 1 of string := init ("Tony", "Dan")
put foo (myArguments2 )
var myArguments3 : array 0 .. 4 of string := init ("Tony", "Dan", "Martin", "compsci", "Ruby")
put foo (myArguments3 )
|
|
|
|
|
|
![](images/spacer.gif) |
Jay
|
Posted: Tue Feb 22, 2005 4:41 pm Post subject: (No subject) |
|
|
Thanks for the help. |
|
|
|
|
![](images/spacer.gif) |
|
|