# Define functions

I want to know how to define functions. I dont mean a function in the sense f[x_]:=x^2 for example, but I mean a function which takes parameters and returns something. Like in C++, you can define such a function as

int function(int parameter){
//do something
return something;
}


and then in the main function we can call this function. How can we do something like this in Mathematica? If I look for function on google, all I get is the function in the sense f[x_]:=x^2.

Edit:

I want to do the following for example. I want the input and output to be polynomials in some variables, and the function is a differential operator. For example, $p(x_1,...,x_n)=x_1^2+...+x_n^2$ and the differential operator is for example $\sum_{i=1}^nx_{n+1-i}\partial x_i$ where $\partial x_i$ takes the derivative of the polynomial with respect to $x_i$.

• f[x_] := x^2 takes one parameter and returns one result. In this respect it behaves just like the C function you showed. I don't understand your question. Perhaps read through this: reference.wolfram.com/language/tutorial/… Oct 28, 2016 at 13:34

Working out the example from the edit:

expr = x1^2 + x2^2 + x3^2 + x4^2 + x5^2;


Extract the variables:

var = Variables @ expr


{x1, x2, x3, x4, x5}

Then compute the sum:

Sum[var[[Length @ var + 1 - i]] D[expr, var[[i]]], {i, 1, Length @ var}]


2 x3^2 + 4 x2 x4 + 4 x1 x5

Those intermediate steps can be gathered into a single function:

operator[input_] := Block[{var},
var = Variables @ input;
Sum[var[[Length @ var + 1 - i]] D[input, var[[i]]], {i, 1, Length @ var}]
]

operator[expr]


2 x3^2 + 4 x2 x4 + 4 x1 x5

In case of expressions like

a x1^2 + x2^2 + b x3^2 + 2 x4^2 + c x5^2


also a, b, c will be treated as variables by Variables. If some symbols are to be treated as parameters, it's probably simplest and safest to manually set which symbols are variables and which are not, like in Sumit's answer below. Also, Variables works well on polynomials, but fails e.g. with this:

Variables @ Sin[x]


{Sin[x]}

• In the function operator[input_] in the second line you write var = Variables @ expr; Dont you have to replace expr with input? Because I want also to input other polynomials Oct 28, 2016 at 13:39
• Yup, good call. Edited. Oct 28, 2016 at 13:41
• I have one more question: I made a table of all the variables I am working with, it is a little different than in the example above. I have variables $p_{i,j}$ where $i$ and $j$ run through some index. I used vars = Table[Subscript[p, i, j], {i, 1, 7}, {j, 1, 3}] to define these variables in a table. How can I use these variables in the Block? I couldnt adjust your code to work with variables in a table. In this case I would have differential operator which is written as a double sum over $i$ and $j$. Oct 28, 2016 at 16:15

Something like this

func[poly_, var__] := Module[{n}, n = Length[var];
Sum[var[[n + 1 - i]] D[poly, var[[i]]], {i, 1, n}]]

func[a x1^2 + b x2^3, {x1, x2}]


2 a x1 x2 + 3 b x1 x2^2