Function Specification: MSC Thai Thuy Han Uyen

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

CHAPTER 3

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 FUNCTION DEFINITION


3
Function_name (argument1: Type1, argument2: Type2…) result:
Type
pre pre-condition
post post-condition

pre: what has to be true before calling


post: what will be true after calling

IMPLICIT SPECIFICATION
4
Example1: multiplication of two natural numbers
Multiply (x: N, y: N) r: N
pre
post r=x*y

pre: what has to be true before calling


post: what will be true after calling

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

pre: what has to be true before calling


post: what will be true after calling

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=(kN1  x=y*k)

FUNCTION CALL
12
Example9: Find the greatest common divisor of two positive
integers

13

You might also like