Professional Documents
Culture Documents
Function Specification: MSC Thai Thuy Han Uyen
Function Specification: MSC Thai Thuy Han Uyen
Function Specification: MSC Thai Thuy Han Uyen
FUNCTION SPECIFICATION
Msc Thai Thuy Han Uyen
1
IMPLICIT FUNCTION DEFINITION
2
Sometimes one does not want/know how to define a function
Implicit function definition allow to express what is to be
computed, not how
IMPLICIT SPECIFICATION
4
Example1: multiplication of two natural numbers
Multiply (x: N, y: N) r: N
pre
post r=x*y
IMPLICIT SPECIFICATION
5
Example2: absolute value of one integer number
Abs (x: Z) r: N
pre
post ((r = x) (x >= 0)) ((r = -x) (x < 0))
IMPLICIT SPECIFICATION
6
Example3: Sum of two positive integer numbers
Sum (x: Z, y: Z) r: Z
pre (x>0) (y>0)
post r=x+y
IMPLICIT SPECIFICATION
7
Example4: solve the first-degree equation (linear equation)
Equation (a: N, b: N) x: R
pre a!=0
post x=-b/a
IMPLICIT SPECIFICATION
8
Example5: Maximum of two natural numbers
Max (x: N, y: N) r: N
pre
post ((r=x) (x>=y)) ((r=y) (y>x))
IMPLICIT SPECIFICATION
9
Example6: maximum of two positive real numbers
MaxPositive (x: R, y: R) r: R
pre (x>0) (y>0)
post ((r=x) (x>=y)) ((r=y) (y>x))
IMPLICIT SPECIFICATION
10
Example7: maximum of 3 positive real numbers
Max3Positive (x: R, y: R, z: R) r: R
pre (x>0) (y>0) (z>0)
post r=MaxPositive(MaxPositive(x,y),z)
FUNCTION CALL
11
Example8:
Is a positive integer divisible by a positive integer?
DivisibleBy (x: N1, y: N1) r: B
pre
post r=(kN1 x=y*k)
FUNCTION CALL
12
Example9: Find the greatest common divisor of two positive
integers
13