# Function addition - Declaring function as the Total of a list of functions

I'm having a problem concerning function addition. The following code shows an example that illustrates my problem. I had this list of functions that have the same variable. I had them by using the Total function and declared a function of that variable equal to the Total[list], like in this example:

f1[x_] := 2 x + 1;
f2[x_] := x;
f3[x_] = 4 x;
l1 = List[f1[x], f2[x], f3[x]];
o[x_] := Total[l1];
o[1]


The output of o[1] was 1+7x, instead of 8, which is what I want. Any suggestion on how to approach this problem? Thank you

• If you do this, you'll get 8 f1[x_] := 2 x + 1; f2[x_] := x; f3[x_] = 4 x; l1[x_] := List[f1[x], f2[x], f3[x]]; o[x_] := Total[l1[x]]; o[1] – Nasser Oct 7 '17 at 19:32
• On top of what @Nasser gave you, the issue with your code has to do with how variables are scoped. The x_ parameter is scoped differently from the x symbol you provide in l1. So you could also mostly keep your code but replace o with this: o[v_] := Total[l1] /. x -> v – b3m2a1 Oct 7 '17 at 19:33
• Or include Evaluate in definition of o[x_], i.e., o[x_] := Evaluate@Total[l1]; – Bob Hanlon Oct 7 '17 at 20:03

Try this (as a side note, when defining functions using SetDelayed one doesn't have to include the semi-colon at the end of the line).

f1[x_] := 2 x + 1
f2[x_] := x
f3[x_] := 4 x


Use SetDelayed (i.e., :=) again to defrine l1

l1[x_] := List[f1[x], f2[x], f3[x]]


Inside the defintion for o use l1 with an argument

o[x_] := Total[l1[x]]


Then test

o[1]
(* 8 *)

o[2]
(* 15 *)


This is similar but not quite identical to what Nasser wrote in his comment.

• Is it possible to get a specific element of the l1 list, now that it is a list of functions? – RicardoP Oct 7 '17 at 23:30
• l1[x][[2]] would, for example, get element 2 – Jack LaVigne Oct 8 '17 at 2:52