I would like to call a user-defined function in any context. To this end I placed it in $BaseDirectory\init.m:

util[x_] := ToString[x] <> "A";

However, to use it in a package it needs to be prefixed with Global.

fun[x_] := Global`util[x + 1];

Is there a way to implement the function so that it is available in any context without Needs or Global?


1 Answer 1


System` is not dropped from $ContextPath by BeginPackage:

 {"Test`", "System`"}

So the way to go can be:

System`util[x_] := ToString[x] <> "A"

Just be careful about names, maybe assume a convention System`custom{name} just in case to avoid collisions.

  • $\begingroup$ This works well. Thanks. $\endgroup$ Jan 24, 2018 at 15:46
  • 1
    $\begingroup$ while this works, I hope you are aware of the fact that now your packages depends on that specific entry in init.m. That will make it very complicated error-prone if ever you want to share or move those packages... $\endgroup$ Jan 25, 2018 at 10:12
  • $\begingroup$ @AlbertRetey I thought this is rather for own use, it should be clear that if you you want to share such feature with someone who may have had the same need then, sooner or later conflicts will happen. Maybe I didn't understand the use case :-/ $\endgroup$
    – Kuba
    Jan 25, 2018 at 10:17
  • $\begingroup$ I was guessing that you and actuall also Chris are well aware of that. I just thought it would make sense to mention it so that other readers will also be warned... $\endgroup$ Jan 25, 2018 at 10:21
  • $\begingroup$ @AlbertRetey you are right, it makes sense. $\endgroup$
    – Kuba
    Jan 25, 2018 at 10:22

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.