Data and processes in computing

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

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.

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, we offer two introductory routes to our qualifications. You could either choose to start with an Access module, or a module which allows you to count your previous learning towards an Open University qualification. Read our guide on Where to take your learning next for more information.

Not ready for formal University study? Then browse over 1000 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 prospectus371