For example if I was defining the following function
exprod[n_] := Expand[Product[x + i, {i, 1, n}]]
Then why is the underscore after the variable n, necessary in the function definition? Where does this style come from or is it specific only to the Mathematica programming language?
The underscore comes from pattern matching.
The x_ matches anything and this anything is bound to the name x in the body of the function.
Then in
l[2*z]first the expression2*zis matched against the patternx_ * y_. Thenxis bound to 2 andyis bound toz. Then the expressionl[x] + l[y]is evaluated, and the result becomesl[2]+l[z].Now say we want to define the value of
loneto be 1. Do we writel[e] := 1orl[e_] := 1?One says that
lto (literally) the variableemust be 1. The other says thatlto something gives 1.http://reference.wolfram.com/mathematica/tutorial/Introduction-Patterns.html