Create a function that can assign values to different variables in Mathematica

Here I am trying to create a function that can assign values to different variables in Mathematica. Assume the function

f[a _, b _, c _];
x1 = a;
x2 = b;
x3 = c;


when I give Mathematica the input f[2, 3, 4]; I would like Mathematica to assign 2 to x1 and 3 to x2 and 4 to x4

After that, for example, if I enter

f[2, 3, 4];
x1+x2


I expect the output to be 7

Which function in Mathematica can assign the number to the variables ? Regards.

• I would suggest that you learn about replacement rules.
– Syed
Commented Dec 28, 2022 at 6:25
• Just saying, the "functional-style" tag is more or less opposite of creating functions with side effects. Also, although Mathematica supports imperative style, it's much more natural to try to explicitly avoid such constructs Commented Dec 28, 2022 at 15:05
• I fully agree with @kirma. Assigning global variables inside a function is a bad idea for several reasons. If you don’t understand why it’s a bad idea, you could probably profit from an introduction to functional programming and programming in general. Commented Dec 28, 2022 at 16:37

We can use a CompoundExpression to do multiple things in sequence:

AssignVariables[a_, b_, c_] := CompoundExpression[x1 = a, x2 = b, x3 = c]


Usually we would use the simpler syntax of ";", but then we need to be careful of precedence. So, it would look like this:

AssignVariables[a_, b_, c_] := (x1 = a; x2 = b; x3 = c)


But this gives slightly weird behavior. If we evaluate

AssignVariables[2, 3, 4]


It actually outputs 4. The variables were successfully assigned, so why this behavior. It's because CompoundExpression resolves to the last statement in the sequence. All statements get evaluated, but the result is whatever the last one produced. Because of this, we often terminate a CompoundExpression with Null:

AssignVariables[a_, b_, c_] := (x1 = a; x2 = b; x3 = c;)


But this is also less than desirable. This function has side-effects, and it gives no indication of what those side effects were. You have to actually read the definition to infer what the results were. So, a better alternative might be to display all of the "results":

AssignVariables[a_, b_, c_] := {x1 = a, x2 = b, x3 = c}


So now if we evaluate it, we get a list as the result, and we have a bit more information about what happened.

AssignVariables[2, 3, 4]


{2, 3, 4}

You could take this further and maybe Print something or create a more complicated output.

Anyway, just to verify that this does the assignments:

AssignVariables[20, 30, 40]


{20, 30, 40}

and

{x1, x2, x3, x1 + x2 + x3}


{20, 30, 40, 90}

Now, having said all of that, it's usually best to avoid functions with this kind of side effect. They introduce surprises in your system. Imagine that x1 was being used somewhere for some other purpose, but you forgot that you were already using it. If you run AssignVariables, you'll maybe break some functionality somewhere else in your code.