Defining Your Own Functions


Functions

Some functions are available for you use. For example, Cinnameg provides sin(x), producing the sine of x, and gcd(m, n), which gives you the greatest common divisor (also called the greatest common factor) of two integers m and n.

But what makes functions really versatile is that you are not limited to the functions that are provided to you out of the box. You can create your own. This chapter begins looking at how to create and use new functions.


Using an equation to define a function

The simplest kind of function definition is an equation where the left-hand side shows the function applied to a variable or variables, and the right-hand side gives its value. For example, definition

  sqr(x) = x*x
defines a function that gives you the square of its argument. So sqr(3) = 9 and sqr(10) = 100. Definition
  happy(y) = 2*y + 1
tells you that happy(4) = 9 and happy(0) = 1.

(Do not write a period after your equation. Just write the equation. Adding a period will result in an error.)

Once a function is defined, you can use it just like you do any other function. For example,

  Let w = sqr(20).
  Let v = happy(5) + 3.
makes w = 400 and v = 14.

When you use a function, the argument can be any expression. For example,

  Let p = happy(8 + 1).
makes p = 2*9 + 1 = 19. The argument expression can even use functions. So
  Let q = happy(happy(2)).
is allowed. (What is q?)


Computation by substitution

Equation

  sqr(x) = x*x
tells you how to square any number. It tells you that sqr(3) = 3*3, sqr(43) = 43*43 and sqr(101) = 101*101. To find out what sqr(v) is, you replace each x on the right-hand side of the equation by v. That is, you do a substitution.

You can always replace a value by one that is equal to it, and that is all that we are doing. For example, to evaluate an expression that contains subexpression sqr(5), replace sqr(5) by the equal value 5*5, and continue. Here is how to evaluate expression sqr(2*4) + 1 in a step by step manner, working from small expressions to larger ones.

  sqr(2*4) + 1
    = sqr(8) + 1
    = (8*8) + 1
    = 64 + 1
    = 65
Here is how to compute 2*happy(9 + 2).
  2*happy(9 + 2)
    = 2*happy(11)
    = 2*(2*11 + 1)
    = 2*(22 + 1)
    = 2*23
    = 46
As a final example, let's compute the value of expression happy(happy(3)), using equation happy(y) = 2*y + 1.
  happy(happy(3))
    = happy(2*3 + 1)
    = happy(6 + 1)
    = happy(7)
    = 2*7 + 1
    = 14 + 1
    = 15


Defining functions with more than one argument

To define a function that has more than one argument, just show all of the arguments, separated by commas. For example, definition

  grape(x,y) = 2*x*y
tells you that grape(10,3) = 2*10*3 = 60. You compute in the same way as before, working from smaller subexpressions to larger ones, and using a function's equation in substitutions.
  3 + grape(3*4, 4+1)
    = 3 + grape(12, 4+1)
    = 3 + grape(12, 5)
    = 3 + (2*12*5)
    = 3 + (24*5)
    = 3 + 120
    = 123


Problems

  1. [solve] Show a step by step evaluation of expression happy(grape(10,4)) + 1.

  2. [solve] Using an equation, define function baste(x) so that baste(x) produces value x2 + 1.

  3. [solve] The distance between two numbers x and y on the number line is |x - y|. Write a definition of function absdiff(x, y), which returns |x - y|. (Recall that abs(v) is the absolute value of v.)

  4. [solve] The distance between two points (u,v) and (x, y) in the plane is sqrt((u - x)2 + (v - y)2). Write a definition of function distance(u, v, x, y) that produces the distance between points (u,v) and (x,y).


Summary

You can create your own functions. Just write an equation that defines the function.


Review

An algorithm is a clear and precise set of instructions telling how to solve a computational problem.

A straight-line algorithm is just a sequence of steps. The steps can be Let-statements. Later we will see other kinds of statements, which can also be used as steps in a program.

A name can contains letters and digits, provided it starts with a letter. A name cannot have any spaces in it. In Cinnameg, it is important to choose names that start with lower case letters.

One use of a name is to avoid recomputation of the same thing several times.