Data and processes in computing
Data and processes in computing

This free course is available to start right now. Review the full course description and key learning outcomes and create an account and enrol if you want a free statement of participation.

Free course

Data and processes in computing

4.4 Functions returning true or false

Consider a function, say ISIN , associated with following process.

Given a character and a string, determine whether the character appears in the string.

This function has two inputs, a character and a string (sequence of characters), so we can take the input set to be Char × SeqOfChar. But what is the output set? If the character does appear in the string, then the function can return the value true, and if the character does not appear in the string, then the function can return the value false. So a suitable output set is Bool. Then ISIN has signature Char × SeqOfCharBool. We can describe the function ISIN as below.

function ISIN (c in Char, s in SeqOfChar) return in Bool

pre   true.

post   The returned value is true if the character c appears in the sequence s at least once, and is false if c does not appear at all in s.

(If s is the empty string, then it contains no characters, and ISIN (c, s) will be false whatever the character c is.)

Activity 20

Suggest a description for a function ISEMPTY corresponding to the following process.

Determine whether or not a given sequence is empty.

Discussion

The function will input a sequence (which might contain elements from any set, say X). It can return true if the sequence is empty, and false if it is not empty. So we can use the following description of ISEMPTY.

function ISEMPTY (s in SeqOfX ) return in Bool

pre   true.

post   The returned value is true if the sequence s contains no members, and is false if s contains one or more members.

M263_1

Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to university level study, find out more about the types of qualifications we offer, including our entry level Access courses and Certificates.

Not ready for University study then browse over 900 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus