Functions are analogous to verbs in human languages: they represent
actions that happen at a particular time.
For example, the built-in +
function takes in two or more numbers, adds them up, and returns the
resulting number; the (user-defined) cube
function takes in
one number, computes its third power, and returns that. Functions
generally accept parameters when they are called and return results.
Data types are more like improper nouns in human languages: they represent a kind of thing. In English, "computer", "student", "dog", "banana", and "program" are kinds of things. In most computer programming languages, there are built-in types like "number", "boolean", "symbol", and "string", while you can define others like "circle", "rectangle", "shape", etc. A data type is not "called" or "invoked" at any particular time on any particular parameters, and it doesn't return a result.
Many programming tasks require inventing both a new data type and a new function (or even several of each). Just deal with them one at a time.
Creating a function | Creating a data type | |
---|---|---|
Analysis & Design (i.e. "decide what you want to do")Implementation ("do it")Correctness Checking ("check that you did it right") |
Describe the new data type in English. In particular, decide whether the new data type is defined by parts or by choices. That is, does an object of the new type "contain one of these, and one of those, and one of the others", or is it "either one of these, or one of those, or one of the others"? For example, a | |
Definition by parts | Definition by choices | |
|
|