Mathematica: How to check if an argument is passed to a function?

How to check if any argument is passed to a function?

For example, if I have:

f[x_Integer]:=1 f[x_]:=Message[errm::err, x] 

and I call f with no argument:

  f[] 

"nothing happens", I want to force a specific condition (errors).

(Background: I do MUnit tests for OO-System packages and classes.)

+8
wolfram-mathematica
source share
2 answers

It?

 f[x_Integer] := 1 f[x_] := Message[errm::err, x] f[] := Message[errm::err] 
+10
source share

Alternatively, by explicitly listing the capabilities of zero-args you can do

 f[x_Integer] := 1 f[args___] := (Message[errm::err, {args}];$Failed); 

which would also catch cases of errors of several arguments passed (provided that this is an error).

+9
source share

All Articles