| Jack Brennen on Thu, 08 Mar 2012 18:44:28 +0100 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: User defined functions: How to program &n-variables? |
Not an exact parallel to issquare() or the other builtins, but
you could try passing the name as a string:
? foo(foo_a,foo_b=0)= {
type(foo_b)=="t_STR" && eval(concat(foo_b,"=floor(foo_a^2)"));
return(foo_a^2);
}
? foo(11.5)
132.25000000000000000000000000000000000
? foo(11.5,"zyx")
132.25000000000000000000000000000000000
? zyx
132
This is pass-by-name, rather than pass-by-reference, but might
work for you. The main limitation is that these won't work
because of name-space issues:
foo(N,"foo_a")
foo(N,"foo_b")
But if you make the foo() parameter names obscure enough, you can
reduce the chance of a name collision to practically zero.
On 3/8/2012 9:18 AM, Thomas Nordhaus wrote:
Hello! I'd like to do something similar as in the issquare-function: How do I code this? For example: I want to program a function foo such that y=foo(x) yields y=x^2 and y=foo(x,&n) yields y=x^2 and n=floor(x^2).Is there a reference that explains proramming of user defined functions in depth? Thanks in advance, Thomas Nordhaus