大象传媒

Implementation (computational constructs)Pre-defined functions

Computational constructs dictate how a source code is structured. Parameter passing, sub-programs, procedures and functions are critical constructs that allow for the development of good software.

Part of Computing ScienceSoftware design and development

Pre-defined functions

Most high level languages will make use of pre-defined functions.

A pre-defined function is built into the software and does not need to be created by a programmer. Pre-defined functions often exist to carry out common tasks, such as:

  • finding an average number
  • determining the length of a string

Modulus function

The modulus (mod) function is another example of a pre-defined function.

It will calculate the remainder of a value that has been divided a set number of times.

For example:

SEND 7 MOD 2 TO DISPLAY

This would print 1 on the screen.

Math round function

The math round function is used to round a number to the nearest integer.

MATH.ROUND(2.99)

This function would round 2.99 to 3.

Substring

The substring function will extract a part from a string.

String.Substring(0, 3)

In this case, the function will take the first three characters. 0 is the index that the function begins from and 3 is how many characters are selected.

Print ord

Print ord(b)
>>> 98

This function will take a given character (b in this case) and then give the corresponding ASCII code for that character. Ord() is a pre-defined function in Python.

Print chr

Print chr(98)
>>> b

This function will take a given ASCII code (98 in this case) and then give the corresponding character for that code.

Related links