Tiny functions

A function is a recipe with a name. Give it inputs, it runs its body, and it hands back a result.

Naming a recipe

How do we make our own thing to call?

With def, a name, the inputs in square brackets, and a block:

def double [n]:
  (* n 2)

double is the name. [n] says the function takes one input, called n inside the body.

How do we use it?

Call it like anything else:

>>> (double 8)
16

The result

Where does the 16 come from?

From the last line of the body. (* n 2) with n as 8 is 16, and that is what the call hands back.

What if the body has several lines?

They run in order, and the last one is the result:

def add-tax [price]:
  var tax (* price 0.08)
  (+ price tax)

The var tax line sets up a helper name. The (+ price tax) line is the result.

The last-line rule

A function’s result is the value of the last line in its body.

More than one input

Can a function take several inputs?

Put each name in the brackets:

def rectangle-area [width height]:
  (* width height)

print "area:" (rectangle-area 6 7)

Do the names matter?

To Koskari, no. To the next person reading, width and height say more than a and b.

A function that chooses

Can a function use if?

A body can hold anything you have used so far:

def describe-score [score]:
  if (>= score 10):
    "great"
  elif (>= score 5):
    "good"
  else:
    "keep practicing"

print (describe-score 7)

The if is the last line, so the value of the branch that ran is the result: good.

What does this print?

def badge [name points]:
  if (>= points 100):
    print name "earned a gold badge"
  elif (>= points 50):
    print name "earned a silver badge"
  else:
    print name "is still warming up"

(badge "Ada" 120)
(badge "Grace" 40)

Ada earned a gold badge, then Grace is still warming up. Change the two numbers and decide again.