Functions - jackbackrack/cons-beginners-course GitHub Wiki
- function is specified in terms of its name, parameters, and code block.
- name -- used for calling it
- parameters -- used for specifying values
- block -- code that is executed when called
function main(args ...) { statements ... return value; }
- invocation
- functions are called using function call syntax
f()orf(1) - inside the function parameters are bound to the values given in the call
- functions are called using function call syntax
examples
Suppose we create houses using explicit polygons as follows:
const h = polygon([-1, -1], [1, -1], [1, 1], [0, 2], [-1, 1](/jackbackrack/cons-beginners-course/wiki/-1,--1],-[1,--1],-[1,-1],-[0,-2],-[-1,-1));
return h;
we can now want write it in a more reusable manner. We can place the house construction in a function and name it house as follows:
function house () {
return polygon([-1, -1], [1, -1], [1, 1], [0, 2], [-1, 1](/jackbackrack/cons-beginners-course/wiki/-1,--1],-[1,--1],-[1,-1],-[0,-2],-[-1,-1));
}
return house();
where we split it into a house function and a house call. Now suppose that we want to make it even more reusable by making the roof height about the square body of the house be a parameter.

We can now add that as a parameter and refer to it inside the house code block:
function house (roofHeight) {
return polygon([-1, -1], [1, -1], [1, 1], [0, 1 + roofHeight], [-1, 1](/jackbackrack/cons-beginners-course/wiki/-1,--1],-[1,--1],-[1,-1],-[0,-1-+-roofHeight],-[-1,-1));
}
return house(2);
which is the same as where the body of house is copied and run with the parameter roofHeight set to 2:
var rootHeight = 2;
return polygon([-1, -1], [1, -1], [1, 1], [0, 1 + roofHeight], [-1, 1](/jackbackrack/cons-beginners-course/wiki/-1,--1],-[1,--1],-[1,-1],-[0,-1-+-roofHeight],-[-1,-1));
Now if I had two calls to house as follows:
var h1 = house(1);
var h2 = house(2);
return union(h1, h2);
it would execute in the same as the following code:
const rootHeight = 1;
const h1 = polygon([-1, -1], [1, -1], [1, 1], [0, 1 + roofHeight], [-1, 1](/jackbackrack/cons-beginners-course/wiki/-1,--1],-[1,--1],-[1,-1],-[0,-1-+-roofHeight],-[-1,-1));
const rootHeight = 2;
const h2 = polygon([-1, -1], [1, -1], [1, 1], [0, 1 + roofHeight], [-1, 1](/jackbackrack/cons-beginners-course/wiki/-1,--1],-[1,--1],-[1,-1],-[0,-1-+-roofHeight],-[-1,-1));
return union(h1, h2);
we can further decompose it and have functions call functions:
function roof () {
return polygon([-1, -1], [1, -1], [0, 1](/jackbackrack/cons-beginners-course/wiki/-1,--1],-[1,--1],-[0,-1));
}
function house () {
return union(square(), roof().translate([0, 2, 0]));
}
return house();
Local Functions
Functions can be written at top level or within other functions:
function house () {
return polygon([-1, -1], [1, -1], [1, 1], [0, 2], [-1, 1](/jackbackrack/cons-beginners-course/wiki/-1,--1],-[1,--1],-[1,-1],-[0,-2],-[-1,-1));
}
function main () {
return house();
}
or
function main () {
function house () {
return polygon([-1, -1], [1, -1], [1, 1], [0, 2], [-1, 1](/jackbackrack/cons-beginners-course/wiki/-1,--1],-[1,--1],-[1,-1],-[0,-2],-[-1,-1));
}
return house();
}
homework
- build a human function with torso, arm, leg, head local functions
- first write functions without parameters
- next write functions with at least one dimension parameter such as length
- write a container function
- where container is a hollow cube with one side open
- takes dimensions and thickness as parameters