MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Checking function syntax

  • To: mathgroup at
  • Subject: [mg64582] Re: [mg64555] Checking function syntax
  • From: jmt <jmt at>
  • Date: Thu, 23 Feb 2006 00:34:19 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at

On Wednesday 22 February 2006 11:58, Chris Rodgers wrote:
> Hi,
> How can I make my own functions give an error if they are called with
> the wrong syntax?

Try this :

Here is your standard definition :
MyFunc1[x_, y_] := Tuples[{x, y}, 2]

Define your error message :
MyFunc1::"WrongInput" = "A sensible error message for incorrect input `1`"

Create the rule that match wrong input :
MyFunc1[u___] := Message[MyFunc1::"WrongInput", u]

Check normal function call :
Map[MyFunc2, MyFunc1[a, b]]

And incorrect one :
Map[MyFunc2, MyFunc1[a, b, c]]

Mind the fact that in some situations some rules might have equal priorities, 
in which case rule definition ordering defines which rule will be called.

  • Prev by Date: Re: Combining ContourPlot and Plot
  • Next by Date: Re: Matrix multiplication problem fixed via dot operation.
  • Previous by thread: Checking function syntax
  • Next by thread: Re: Checking function syntax