Functions

Functions consume data and produce data. We say a function...

consumes its input, and

produces its output.

Data Type

Data type, or just type defines

  1. A set of values
  2. A set of functions that consume those values

number

One data type in Scheme is number

All possible real numbers, like −358277, − 1/3, 0, 1, 21, π, 256 ...

Functions that consume numbers include + − *, and /.

string

What we commonly call text - or a sequence of characters. Anything you can type on a keyboard.

A string is any sequence of characters enclosed in double quotes.

Some examples: "hello", "goodbye", "Nerds are cool.", "123456"

Important A sequence of digits enclosed in quotes is a string, not a number.

 1234 is a number
"1234" is a string

image

A picture or image that can be displayed on the screen

Dr Racket can display many different images, including circles, squares, rectangles, and stars.

Documentation for Dr Racket Image Library

Contracts

A contract shows what a function consumes and produces.

Here is the contract for +

;; + : number number -> number

A contract shows

circle

The circle function consumes a number and two strings and produces an image.

;;circle : number string string -> image

square

The square function also consumes a number and two strings and produces an image.

;;square : number string string -> image

ellipse

The ellipse function consumes two numbers and two strings and produces an image.

;;ellipse : number string string -> image

Function Composition

A function is nested inside another function

Example from Unit 1

×
7
4
1

Example Using Ellipse

ellipse
30
"solid"
"red"
+
20
30
(ellipse 30 (+ 20 30) "solid" "red")
Computer Programming Unit 2.1 - Functions, Data, and Types