return {} when the function is given {}
Actually, I want the output to be {} if there is anything wrong with the argument.
For this I recommend one or more definitions with patterns that only match a valid argument, and a fall-through definition for anything else. For example if the argument should be a nonempty list of integers:
(* primary definition *)
func[arg : {__Integer}] := Mean[arg]
(* fall-through definition *)
_func := {}
test:
func[{1, 2, 3}]
func[{1.23}]
func[{}]
func[1, 2, 3]
2 {} {} {}
Additional reading:
- How to check the style and number of arguments like the built-in functions?
- Quick way to use conditioned patterns when defining multi-argument function?
One way is to make some definitions
foo[arr_] := {}
foo[{}] := {}
foo[arr_List] := "ok"
Mathematica will automatically pick the correct definition to use.