Function Preconditions and Postconditions
Functions may have preconditions and may have postconditions. Preconditions and postconditions can be used to restrict the inputs (values for parameters) and output (return value) of a function.
Preconditions must be true right before the execution of the function. Preconditions are part of the function and introduced by the pre
keyword, followed by the condition block.
Postconditions must be true right after the execution of the function. Postconditions are part of the function and introduced by the post
keyword, followed by the condition block. Postconditions may only occur after preconditions, if any.
A conditions block consists of one or more conditions. Conditions are expressions evaluating to a boolean. They may not call functions, i.e., they cannot have side-effects and must be pure expressions. Also, conditions may not contain function expressions.
Conditions may be written on separate lines, or multiple conditions can be written on the same line, separated by a semicolon. This syntax follows the syntax for statements.
Following each condition, an optional description can be provided after a colon. The condition description is used as an error message when the condition fails.
In postconditions, the special constant result
refers to the result of the function.
In postconditions, the special function before
can be used to get the value of an expression just before the function is called.
Last updated