Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have build this simple module trying to construct a matrix via Table

ImagePoint[\[Sigma]_] :=
  A = Table[i^2 + \[Sigma], {i, 10}];

In[74]:= B = ImagePoint[0.67]
In[76]:= B
In[75]:= ?B

Doing the same but outside the module works fine:

In[78]:= DD = Table[i^2 + 0.67, {i, 10}];
In[65]:= ?DD

Can someone explain the difference and what am I doing wrong?

share|improve this question

closed as too localized by F'x, rm -rf, Artes, Simon Woods, Oleksandr R. Sep 3 '12 at 0:53

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers 3

As you have ended the last (and only) statement in your Module with a semicolon it is not producing any output. A semicolon is used for CompoundExpression in MMa and will return Null. To achieve what you wanted define your Module like this:

imagePoint[sigma_] := Module[{}, Table[i^2 + sigma, {i, 10}]]

or as you don't need local variables

imagePoint2[sigma_] := Table[i^2 + sigma, {i, 10}]
share|improve this answer

You're assigning a value to A, which is a local variable inside your module and cannot be seen outside. Also, because you're ending this expression with a semicolon, the value is not returned. Change your function to be either:

ImagePoint[σ_] := Module[{}, Table[i^2 + σ, {i, 10}]]

or use an explicit return:

ImagePoint[σ_] := Module[{A}, A = Table[i^2 + σ, {i, 10}]; Return[A];]

or at least:

ImagePoint[σ_] := Module[{A}, A = Table[i^2 + σ, {i, 10}]]

(where the assignement to A is useless, really, but doesn't hurt either).

share|improve this answer

This tutorial Sequences of Operations will help you to understand your mistakes. Proper ways to set up your function:

ImagePoint[s_] := Table[i^2 + s, {i, 10}]

If you have to use Module due some reason make sure it returns something - so remove semicolon. And there is no reason to introduce a dummy local variable A inside Module if you are not planning to use it.

ImagePoint[s_] := Module[{}, Table[i^2 + s, {i, 10}]]

It will work now:

B = ImagePoint[.6]

{1.6, 4.6, 9.6, 16.6, 25.6, 36.6, 49.6, 64.6, 81.6, 100.6}


enter image description here

share|improve this answer
thank you all for the help –  Doron Sep 2 '12 at 9:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.