I want to define a function which can take only integer arguments and have found two ways to do the same :
func1[n_Integer, m_Integer] := If[n==m,0,n-m]
func2[n_?IntegerQ, m_?IntegerQ] := If[n==m,0,n-m]
I have also read about the difference between the two : here. Both of the above ways ensure that the functions are not evaluated if the argument is not having a Head
as Integers
.
However, I would like the functions to return 0
if the arguments are not integers instead of being unevaluated. One way to do this would be using a nested-if (not sure about the terminology in Mathematica
) :
func3[n_, m_] :=
If[IntegerQ[n] && IntegerQ[m], If[n == m, 1, n - m], 0]
Is there any way other than using the above to achieve the same result? Also, if I use func1
and func2
in an integral etc such that their arguments are [5,5.2]
then am I going to receive any warning of the non-evaluation or that particular value is just going to be skipped?
If
statement? Adapting the answer by bill s, maybe:f[n_?IntegerQ, m_?IntegerQ] := n-m; f[n_, m_] := 0; f[n_?IntegerQ, n_?IntegerQ] := 0;
$\endgroup$