1

Can we return a path or picture from a vardef macros? And how to call the return value? Google said "the last statement has no semicolon; its value is returned by the function". My purpose is to repeat using the path or picture. Followed is a small code. But it failed. Maybe I misunderstood something. Please note that the returned path or picture might not need to be drawn out. It might be used to help to construct something else, e.g., help to find intersection point.

vardef foo (expr from, to) =
  path p;
  p = from -- to
enddef;

beginfig(1)
  path p;
  pair A, B;
  A = (0,0);
  B = (1,1);
  p = foo (A, B);
  draw p;
endfig;
  • A couple of observations: (a) be careful about declaring variables inside a vardef; they look local but they are not; the path p; inside your vardef will remove the value of p outside it. (b) there is no "residue" from an equation, so there's nothing to return from the final statement of your vardef. So MP returns vacuous, and I guess you got an error saying Equation cannot be performed (path=vacuous). – Thruston Oct 21 '17 at 22:57
  • @Thruston Yes, you are right. The error is vacuous. – warem Oct 22 '17 at 2:40
3

You can, and there is no need to specify the type of the returned object or even to give it a name. This should work for you as well:

vardef foo (expr from, to) =
  from -- to
enddef;

beginfig(1)
  path p;
  pair A, B;
  A = (0,0);
  B = cm*(1,1);
  p = foo (A, B);
  draw p;

endfig;
end.

enter image description here

Edit: here is an equivalent but much shorter coding of the same program, without variables which are unnecessary in this simple case.

vardef foo (expr from, to) =
  from -- to
enddef;

beginfig(1)
  draw foo (origin, cm*(1,1));
endfig;
end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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