COM555
Theory of Computation
Lecture 3
Functions
Assoc Prof Dr Melike Sah Direkoglu
[email protected] Functions
• A function is an object that sets up an input-output relationship.
If f is a function whose output value is b when the input value is
a, write it as
f(a)=b
• A function is also called a mapping. If f(a)=b, we say that f maps
a to b.
• Ex. Add function; takes pairs of numbers and the output is the
sum of those numbers:
add(x,y)= x + y
• Ex. Absolute function; takes a number x as input and returns
nonnegative x value
abs(-2)=abs(2)=2
Functions – Domain and Range
•• The
set of possible inputs to the function is called its domain.
• The outputs of the function comes from a set called its range.
• The notation for saying that f is a function with domain D and
range R is
f: D R
• Eg. The abs function takes integers as input (Z), domain is Z,
and produces an output, range is Natural numbers (N).
abs: Z N
• Eg. The add function for integers; the domain is the set of
pairs of integers, Z x Z, and the range is integers, Z.
add: Z x Z Z
Functions – Using Table
•• One
way to describe a function is using a table that lists
all possible inputs and gives the output for each input.
• Eg. f: {0, 1, 2, 3, 4} {0, 1, 2, 3, 4}. We can also write f as
f: n f(n)
0 1
1 2
2 3
3 4
4 0
f adds 1 and then takes modulo (mod) 5.
• What is function f? Mod 5 is remainder after division to 5.
Functions – Using Table (Cont.)
• Eg.
Two dimensional table is used if the domain of
the function is the cartesian product of two sets, such
as g: x
• The entry at row i and column j is the value of g(i,j).
Such as g(0,0)=0, g(2,1)=3
g 0 1 2 3
0 0 1 2 3
1 1 2 3 0
2 2 3 0 1
3 3 0 1 2
• What is function g? g= ( i + j ) % 4
addition function modulo 4
Arity of Functions
• When
the domain of a function is for some
sets ,…, , the input to f is a k-tuple ( and we
call arguments to f.
• A function with k arguments is called a k-ary
function, and k is called the arity of the
function.
• If k=1 (singe argument), the f is called a unary
function.
• If k=2, f is called a binary function.
Predicate/Property
• A predicate or property is a function whose
range is {TRUE, FALSE}.
• Eg. even is a property that is TRUE if its input
is an even number and FALSE if its input is an
odd number.
– even(4)=TRUE, even(9)=FALSE
• A property whose domain is a set of k-tuples
Ax…xA is called a relation, a k-ary relation,
such as the example beats game.
Predicate/Property (Cont.)
• Eg. Beats game have values of TRUE or FALSE.
beats scissors paper Stone
Scissors FALSE TRUE FALSE
Paper FALSE FALSE TRUE
stone TRUE FALSE FALSE
• beats: {(scissors, paper), (paper, stone),
(stone, scissors),…,}{TRUE, FALSE}