[Date Index]
[Thread Index]
[Author Index]
Re: returning variable number of arguments from a Module[ ]
*To*: mathgroup at smc.vnet.net
*Subject*: [mg71498] Re: returning variable number of arguments from a Module[ ]
*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
*Date*: Mon, 20 Nov 2006 18:12:01 -0500 (EST)
*Organization*: The Open University, Milton Keynes, UK
*References*: <ejs38c$94b$1@smc.vnet.net>
bd satish wrote:
> Hi ,
>
> Is there any method to return a variable number of arguments from a
> Module[ ] or Block[ ] depending upon the o/p list ?
If I have understood correctly what your desire, the straight answer is
a plain "No". Basically, in an assignment such as expr1 = expr2, the
RHS expr2 is not aware of the LHS. That is, expr1 might or might not
exist, it could be a single symbol such as W, a list with only one
element such as {W}, a list of several elements such as {W, Y}, etc.
Therefore, expr2 is going to be evaluated first and returns whatever it
has to return. If there is no assignment, the value(s) is/are just
displayed (yet they are still assigned to the variable Out]. If expr1
exists, then the value(s) returned by expr2 are going to be assigned to
expr1 by the function Set in the same order they have been returned.
> For example, consider a module
>
> f[x_,y_]:= Module[ {t,s} , t=x+y; s = t*y ]
>
> Also assume that , the variable 's' has higher priority than 't' . So when
> a user types a single o/p variable i.e.
>
> W = f[x,y] then W should have the same value as 's' in the
> module
>
> { W, Y } = f[x,y] then W points to 's' and 'X' points to 't'
What is going to be returned by a user defined function is the last
expression of the function. Therefore, returning several values can be
done by writing the last expression as a list of expressions and/or
values. You have several ways to get the desired result. You will find
below some of these methods.
In[1]:=
f[x_, y_] := Module[{t, s}, t = x + y; s = t*y; {s, t}];
{W, Y} = f[x, y]
Out[2]=
{y*(x + y), x + y}
In[3]:=
Clear[W, Y, f];
f[x_, y_, r_:1] := Module[{t, s}, t = x + y; s = t*y; If[r == 1, s, {s,
t}]];
f[x, y]
{W, Y} = f[x, y, 2]
Out[5]=
y*(x + y)
Out[6]=
{y*(x + y), x + y}
In[7]:=
Clear[W, Y, f];
f[x_, y_, 1] := Module[{t, s}, t = x + y; s = t*y];
f[x_, y_, 2] := Module[{t, s}, t = x + y; s = t*y; {s, t}];
f[x_, y_, 3] := Module[{t, s}, t = x + y; s = t*y; {t, s}];
f[x, y, 1]
{W, Y} = f[x, y, 2]
f[x, y, 3]
Out[11]=
y*(x + y)
Out[12]=
{y*(x + y), x + y}
Out[13]=
{x + y, y*(x + y)}
In[14]:=
Clear[W, Y, f];
f[1][x_, y_] := Module[{t, s}, t = x + y; s = t*y];
f[2][x_, y_] := Module[{t, s}, t = x + y; s = t*y; {s, t}];
f[3][x_, y_] := Module[{t, s}, t = x + y; s = t*y; {t, s}];
f[1][x, y]
{W, Y} = f[2][x, y]
f[3][x, y]
Out[18]=
y*(x + y)
Out[19]=
{y*(x + y), x + y}
Out[20]=
{x + y, y*(x + y)}
HTH,
Jean-Marc
Prev by Date:
**Re: Why does this lead to an answer with complex numbers?**
Next by Date:
**Re: Arithmetic Puzzle (so simple it's hard)**
Previous by thread:
**Re: returning variable number of arguments from a Module[ ]**
Next by thread:
**Re: Re: returning variable number of arguments from a Module[ ]**
| |