MathGroup Archive 2006

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

Search the Archive

Re: Checking function syntax


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