Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My basic need is I have to take lots of data and other information and assemble it into a function (static parameters). I then need to call the function many times for numerical results for many values of the dynamic parameters. I then need to repeat with a new set of static parameters. I have a workable solution that I'm using now but it does not feel like a slick solution. So I'm looking for some fresh ideas.

MakeRegion[id_, g_, geo_] := (
  listend = #[[-1]] & /@ (Select[
     FindShortestPath[g, id, #] & /@ (Pick[VertexList[g], 
        VertexOutDegree[g], 0]), Length[#] > 0 &]);
  cond = Table[
     {crf, rrf, typerf, ptsrf} = {ApCenter, ApR, Type, ApPoints} /. geo[[listend[[i]]]];
        typerf == "Polygon", PointInPoly[ptsrf, {x, y}],
        typerf == "Circle", (x - crf[[1]])^2 + (y - crf[[2]])^2 <= r
      {i, 1, Length[listend]}];
  region[x_, y_] := Evaluate[Or @@ cond];

I do not think I need to go into the details of the code for you to get the idea. MakeRegion takes in geometry information and creates the function region[x,y] which is a logical domain that I can use in NIntegrate and other functions.

Here is a simple example:

example[a_] := (
   fun[x_, y_] := 
      Evaluate[x Total[RandomVariate[NormalDistribution[], a]] + y ];

The function example has no return value, but it defines the function fun, which I can use until I need to get a new instance of the function fun which I do by running example[a].

share|improve this question
your function MakeRegion can't work : It doesn't return anything because of the semi-colon at the end of the last but one line – andre Feb 23 '13 at 12:53
Yes the function MakeRegion does not have a return value but it does define the function region which is the function that I use for the numerical calculations. Here is a simple proof of concept example: Clear[example] example[a_] := ( Clear[fun]; fun[x_, y_] := Evaluate[x Total[RandomVariate[NormalDistribution[], a]] + y ]; ); – c186282 Feb 23 '13 at 13:14
Here are two links to posts using closures. You might find these interesting. 1 and 2 – Szabolcs Feb 24 '13 at 21:09
up vote 15 down vote accepted

The more Mathematica-like way of doing this is to actually return a function, rather than define a single global function as a side-effect:

createFunction[a_] := 
        x Total[RandomVariate[NormalDistribution[], a]] + y 

Now you can create as many functions parameterized by a as you like, assign those functions, pass them other places:

{NIntegrate[f3[x, y], {x, 0, 5}, {y, 0, 4}]], Plot[f9[x, 5.], {x, 0, 5}]}
share|improve this answer
Thank you. I had originally tried something like this but I made the silly mistake of doing f3:=createFunction[3]. Note, improper usage of := – c186282 Feb 23 '13 at 17:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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