# Indexed variable - Function that contains summation

I have the following problem: I have a function that looks like $$f(c_1 , c_2 ) = c_1 + c_2 + c_3$$. The variables are $$c_1$$ and $$c_2$$, $$c_3$$ is a known constant. I would like to define a function in mathematica that resembles this $$f$$ by telling the compiler that $$c_1$$ and $$c_2$$ are my variables, but $$c_3$$ is not! My first approach looked like this:

Subscript[c,3] = 2
f[Subscript[c,1],Subscript[c,2]] := Sum[Subscript[c,i],{i,1,3}]

However, as a test, f[1,2] does not return the number 5 as it should.

I have read other posts regarding this topic but have not understood them, because I am a complete beginner. Would appreciate your help.

• 1. Don't use Subscript for computation; they are prone to weird behavior. Use indexed variables instead, like c[1], c[2]. 2) You don't have to reproduce the notation if it makes your life harder; you could use f[a_, b_] := a + b + c[3] Commented Mar 30, 2021 at 2:46

Input variables of functions in Mathematica should have _ at the end of their names. Using Subscript in Mathematica is a little tricky and sometimes leads to unexpected results. It's recommended to not use them. Also, you can't use _ in your variables, except in inputs of functions.

But for your problem, I will show some alternatives:

### Solution 1

c3 = 2
f[c1_, c2_] := c1 + c2 + c3

f[1, 2]
(*Out: 5 *)

### Solution 2

c3 = 2
f[c1_, c2_] := Sum[i, {i, {c1, c2, c3}}]

f[1, 2]
(*Out: 5 *)

### Solution 3

If you'd defined your variables in c1, c2 format:

c3 = 2;
f[c1_, c2_] := Evaluate[Sum[Symbol["c" <> ToString[i]], {i, 1, 3}]]

f[1, 2]
(*Out: 5 *)

This solution will use Symbol to automatically access variables by building their names as string.

• Thank you for your helpful and quick answer! I missed a very important point: The function I stated above is more a trivial function in order to ask the question more easily. The actual function I am dealing with involves an arbitrary amount of c_k, where c1 and c2 are my variables and all remaining c_k are constants. Also, they are not just summed over with plusses as my function above, but are computed in a more complex way. So is there a general way do define a function with subscript variables, where I tell the compiler that only two specific of them are actual variables? Commented Mar 29, 2021 at 17:09
• @MegAmaNeo1 As far as I know, inputs of function should be symbol without any wrapper like Subscript. for your problem, I added solution 3. Commented Mar 29, 2021 at 17:22
Clear["Global`*"]

I recommend that you avoid the use of subscripted variables and instead use indexed variables. You can display the indexed variables in any desired manner using Format.

Format[c[n_]] := Subscript[c, n]

n = 5;

var = Array[c, n]

To set variables c[3] through c[n] equal to a constant

Set[c[#], k] & /@ Range[3, n];

Then

var

To use the variables as arguments to a function

f @@ var

Total@var

Sum[c[m]/m!, {m, 1, n}]