You can explicitly define variables in the global context by prefixing their name with
Global`, for example,
Global`i = 3 or
Global`f[x_]:=x^2. However if you have set the notebook to have private context, you don't have
Global` in your
$ContextPath (in order to prevent interference from other notebooks with non-private context). Therefore in your notebooks the context
Global` isn't really special, you can use any context, like
myShared`. Indeed, that's desirable because that way you know that a third notebook will be unlikely to interfere.
So you would write in your first notebook
and then could access that function from the other notebook as
Note that you can add your context to the context path by using
This then allows you to refer to the function above without the prefix, e.g.
However note that with this, your local symbols may shadow the shared ones (however access as
myShared`function still works even then), so you have to make sure that you don't use the unprefixed symbol in any way before the prefixed one was created.
You might consider avoiding the issue by using
PrependTo instead of
AppendTo, but then you are vulnerable to variable injection (including accidental one) from the other notebook. For example, imagine that you have the definitions
a = 3;
and then you do in your other notebook
function[a_] := a^2
other[a_] := 5
Let's assume the symbol
a had not yet been used in that notebook, then it is created, together with the symbols
other, in the context
myShared`, and therefore in your first notebook now hides the local definition of
a. That is, if you now evaluate
a in your first notebook, the kernel will find
myShared`a (without a value) first, and therefore use that instead of the local
a; of course it won't evaluate to 3.
Yet another way to access the symbol without prefix would be the definition
function := myShared`function
which however only works in a context with evaluation. Especially it should not be used for shared variables because after
variable := myShared`variable
a subsequent assignment like
variable = 42
does not change the shared variable, but only the local one (which no longer refers to the shared one).
Therefore I think it is a better idea to not do either, but always use the prefixed version.
Note that the
PrependTo scenario is also an argument against using
End to simplify definitions in this case: It's too easy to accidentally introduce new symbols in that context which were not intended to be there.
Note that another way of sharing functions is to make them into a package and use that package from both notebooks. Ultimately this also boils down to having a shared context, as rcollyer noted in the comments. However a properly written package protects against or at least warns about most problems with hiding. Of course, writing a package might be overkill for your specific situation.