28
votes
Accepted
How can I make assignments persist across sessions?
If you wrap your definitions in Once then their results will be remembered across sessions:
...
26
votes
How to make Mathematica variables declarative instead of just-in-time?
You are looking for $NewSymbol which is run every time a new symbol is created. For example, let say you only want x, ...
25
votes
Accepted
How can one manually change the rule ordering
General
The definitions get reordered at definition-time by a part of the pattern matcher, that takes care of automatic rule reordering. It does so, based on relative generality of rules, as far as ...
22
votes
How can one manually change the rule ordering
Actually we have direct control over this via a System Option. Set:
SetSystemOptions["DefinitionsReordering" -> "None"];
Then:
...
22
votes
How to pass variables by reference and by value
You asked for a general explanation instead of just focusing on specific application examples, so here it goes ...
The concepts of "pass by reference" and "pass by value" that you ...
20
votes
Accepted
Why does the global variable exist here, where it was declared only in Module?
Why is this happening
The explanation was basically given by ciao in comments. You can also find a lot of information on this in this great answer of Mr.Wizard. I will perhaps try to view it from a ...
17
votes
How to tell Mathematica that certain variables are real/imaginary, integer-valued, etc
$Assumptions = n > 0 && n ∈ Integers && L ∈ Reals && L > 0
You can type ∈ symbol in the form <...
17
votes
Accepted
Working with DynamicModule: SetDelayed + OwnValues
J. Fultz' answer indicates that it is not a bug but a feature which needs better documentation.
Get a habit of defining your procedures with [], e.g. ...

Kuba♦
- 136k
16
votes
Accepted
Attempts at backwards compatibility
If you just want backwards compatibility, there is no need to create any symbols in the System context. It seems like a bad idea to do such things. A package should create symbols only within its ...
16
votes
Accepted
How to clear all definitions matching f[1, ...] but not f[2, ...]?
You can modify DownValues[f] directly. For example
...
16
votes
Accepted
When is a symbol a Symbol? Is there an easy Mathematica way to test if an object is a symbol sort of like a SymbolQ?
I'd probably use x_Symbol in a function argument to control evaluation. Otherwise, one might do the following (thanks to @Leonid for pointing out an oversight).
If ...
15
votes
Accepted
Confusing behavior when passing a variable vs. inlining a function call
Mathematica is an expression rewriting language. When it evaluates:
q[[RandomInteger[{1, 2}]]] += 1
It first rewrites it as:
...
15
votes
Accepted
very simple but unexpected aspect of Function[ ]
The issue is that to SetDelayed1 (:=), Function is just a normal head. For your third ...
14
votes
Accepted
How to make Mathematica variables declarative instead of just-in-time?
Edit: method extended for multiple contexts and unlocking mehtod added.
Let's protect whatever is a new symbol.
In old answer I've manually excluded symbols matching ...

Kuba♦
- 136k
13
votes
Finding a variable name given its numerical value
a = 1;
Select[Names["Global`*"], Symbol[#] == 1 &]
{"a"}
Notice it may not be robust if you have a habit of writing ...

Kuba♦
- 136k
13
votes
Accepted
When to name a variable with $
Look at the system context to see what the convention is. I can find 257 built-in symbols in 11.3,
symbols = Names["System`$*"];
Length @ symbols
(* 257 *)
In ...
Community wiki
12
votes
How can I make assignments persist across sessions?
Like in all other systems I am familiar with, variable and function definitions exist in memory (RAM) only and do not persist across sessions.
If you want a definition to persist, you must save it ...
12
votes
When is a symbol a Symbol? Is there an easy Mathematica way to test if an object is a symbol sort of like a SymbolQ?
How about:
SymbolQ[_Symbol] = True
SymbolQ[_] = False
?
11
votes
Accepted
How can I define variables that follow a naming pattern?
Here's a pretty natural way, I think, that almost matches your variable names.
...
11
votes
Cannot get determinant of matrix
Mathematica does not distinguish between row and column vectors (...
11
votes
Accepted
Is it possible to define conjugate of a function?
Conjugate[f[x_]] := fc[x]; will fail because MMA tries to store this rule under Conjugate, which is protected.
However, you may ...
10
votes
How can one manually change the rule ordering
This is more of an extended comment in response to
@LLlAMnYP is there a way to do this without knowing the existing definitions in advance? or maybe use Prepend?
You can roll a function like so:
<...
10
votes
How to Set parts of indexed lists?
This is not a precise answer to the actual question, but the list of answers is from 2012 and IMO does urgently lack a note about Association. Since version 10 (...
10
votes
How can one manually change the rule ordering
Updated
Updated twice, once to force autoloading, and a second time to eliminate duplicate *Values
I will answer the last part of the question, specifically:
Or conversely to make sure a definition ...
10
votes
Accepted
`Quit` vs `ClearAll["Global`*"]`
There are significant practical differences.
Quit does not "clear" anything, it instead restarts the kernel, i.e. resets it to the default state. There ...
10
votes
Accepted
How do I insert OwnValues inside a held expression without evaluating it?
ClearAll[a, b];
a := 1 + 1
b = Sqrt
Is this acceptable?
...

Kuba♦
- 136k
10
votes
Accepted
Can I use a graphic as the name of a variable?
You may use the Notation, Symbolize, and InfixNotation tutorial to define your own notation. This method has the added bonus of not unprotecting built-in symbols.
Below is a screenshot of the ...
10
votes
Different strategies to get a clean Kernel. Quit, Exit, ClearAll, Remove, CleanSlate?
PREVENTION
This is a Community Wiki answer, experienced users are welcome to add to it and correct mistakes.
Best practice is to avoid the need to clean the kernel at all, by avoiding the creation ...
10
votes
Accepted
KeyExistsQ[] but for functions?
Here's a simple method for checking if any of the down values of a function matches f[5] literally. It's a good use case for ...
9
votes
How to differentiate formally?
Starting in M11.1, this works:
sum[n_] = Sum[i x[i],{i,1,n}];
D[sum[n],x[2]] //InputForm
Piecewise[{{2, n >= 2}}, 0]
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
variable-definitions × 502functions × 75
function-construction × 63
variable × 52
scoping × 37
list-manipulation × 36
evaluation × 32
equation-solving × 25
assignment × 23
symbols × 22
plotting × 21
matrix × 19
syntax × 16
replacement × 15
programming × 14
manipulate × 13
symbolic × 13
core-language × 12
differential-equations × 10
calculus-and-analysis × 10
pattern-matching × 10
warning-messages × 10
complex × 10
packages × 10
clear × 10