# Assigning a value to a functions argument

I´m struggeling now a few hours on a very basic problem. I want to assign the value 0 to variable i, if i has another value, then it should be changed to 0 in the function.

Function

 ClearAll["Global*"]
ExpandSum[summand_, range_List] :=  Total[(Function @@ {range[[1]],HoldForm[summand]}) /@ Array[range[[2]] - 1 + # &, (range[[3]] - range[[2]] + 1)]]


Input:(It works perfectly with that:)

 ExpandSum[i^4, {i, 3, 20}]


It works not with that:

 i = 10;
ExpandSum[i^4, {i, 3, 20}]


I tried clear[i], but it doesnt work. i=0 leeds to "Cannot unset raw object 10). tried to set i_Object or i_Integer - no help.

Sorry for that very basic question but I´m stuck...

Many thanks!

• It is not really clear what you want to achieve. First, Table is a built-in symbol, so you "cannot" change its definitions – you should use some other symbol. Second, I suppose you are not showing us your actual code with the actual problem. Please edit the question and include your code that will clearly show what is the input and what is the desired output. May 3, 2023 at 12:08
• @Domen I added the function. May 3, 2023 at 12:34
• Please look up docs for Module and With. Set i inside the module (locally) to zero and it doesn't need to be an argument bringing in a global value to the function.
– Syed
May 3, 2023 at 15:58

Roberta, this is essentially the same problem as Passing argument to a function

You seem to be overthinking these scenarios. You're trying to layer abstractions on top of the abstractions that already exist and could be used directly.

 ExpandSum[i^4, {i, 3, 20}]


Now, just from a design perspective, there is an immediate problem: you must pass in i in both arguments. That kind of repetition should be a big red flag. But other than that, we're right back to the problem from your other question, which is that you need to figure out how to control i when it's inside your function so that it doesn't take on its expected value from outside your function. As soon as the global i gets a value, your function will break if you try to pass in expressions with i. There are ways you could do that, but they are just way more complicated than just letting the built in functions do this for you:

theRange = Range[3, 20];
theFunction = HoldForm[#^4] &;
theResult = Total[theFunction /@ theRange]


Or:

Total@Table[theFunction[index], {index, theRange}]
(*which is equivalent to ...*)
Total@Table[theFunction[index], {index, 3, 20}]
(*which is equivalent to ...*)
Total@Table[HoldForm[#^4] &[index], {index, 3, 20}]


I'm not sure if you really wanted HoldForm in there or if you were just debugging. These could be simplified without the HoldForm.

If you do this:

ExpandSum[summand_, range_List] := Block[{},
Echo[range];
Total[(Function @@ {range[[1]], HoldForm[summand]}) /@
Array[range[[2]] - 1 + # &, (range[[3]] - range[[2]] + 1)]]
]


Then you will see that your function is operating on {10,3,20}

So the function that ends up being mapped is function ends up being mapped

Function[10,10000]/@yourList


Perhaps you are looking for something like:

Total[HoldForm[#^4] & /@ Range[3, 20]]
`
• PS. Clear[i] works for me. It doesn't give the Unset warning. May 3, 2023 at 13:46
• Many thanks, but it doenst work. Range[1] is 10 but should be i May 3, 2023 at 14:18