This is my first question. I'm fairly new to Mathematica and completely new to SE, so I apologize if this question is trivial.

I'm working on a problem that requires a function to be redefined several times and applied to data. My "function generator" (not sure if this is the proper application of this term) looks like this:

makeLine[{m_, b_}] := line = Function[{x, y}, y - m x - b]

The idea being that the program can call line several times and then redefine it and call it again.

I was hopeful that I could do something like this:

Module[{line}, makeLine[{1, 0}]]

to avoid defining line as a global function, but it doesn't work.


line = Function[{x\$, y\$}, y\$ - 1 x\$ - 0]

So, is it possible to do this? If not, can someone suggest an alternate construct that does essentially the same thing?

  • 3
    $\begingroup$ You might prefer to make the name an argument to makeLine. Could do via SetAttributes[makeLine, HoldRest]; makeLine[{m_, b_}, line_] := line = Function[{x, y}, y - m x - b] $\endgroup$ Commented Mar 14, 2013 at 3:30
  • $\begingroup$ @DanielLichtblau Thank you. $\endgroup$
    – Haer'Dalis
    Commented Mar 14, 2013 at 9:16

2 Answers 2


As Daniel describes in a comment you could make the symbol to define a parameter of makeLine. Just to show a different way I'll use a SubValues pattern:

SetAttributes[makeLine, HoldAll]
makeLine[sym_][{m_, b_}] := sym = Function[{x, y}, y - m x - b]

 makeLine[line][{5, 2}];
 Array[line, {5, 2}]
{{-6, -5}, {-11, -10}, {-16, -15}, {-21, -20}, {-26, -25}}

However, it may be simpler to just define the symbol locally yourself:

makeLine[{m_, b_}] := Function[{x, y}, y - m x - b]

 line = makeLine[{5, 2}];
 Array[line, {5, 2}]
{{-6, -5}, {-11, -10}, {-16, -15}, {-21, -20}, {-26, -25}}
  • $\begingroup$ I cannot yet "+1" Mr. Wizard or I would. Thank you for your help. $\endgroup$
    – Haer'Dalis
    Commented Mar 14, 2013 at 9:15

Block works as you expect.

Block[{line}, makeLine[{1, 0}]]

Module probably does not work, because it will only fake scoping for values that appear literally in it's body (ie so called lexical scoping).

Block has "dynamic" scoping. More info here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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