Previous Up Next

10.2.4  Checking conditions with assert

You can break out of a function with an error by using the assert command, which takes a boolean as an argument. If the boolean is false, then the function will return with an error. For example, if you define a function

  sqofpos(x) := {assert(x > 0); return x^2;}

then if you enter

sqofpos(4)

you will get 16, but

sqofpos(-4)

will return an error, since -4 > 0 is false.


Previous Up Next