Skip to main content

How To Use Functions

TypeScript is the programming language used in the Code Editors of DynaMaker.

Logic & Syntax

Functions work like given some input, do something and optionally return output:

function anyFunctionName(input) {
// do something
return output
}

A function could have multiples or none inputs too, as example:

function anyFunctionName(input1, input2, input3) {
// do something
return output
}

function anyFunctionName() {
// do something
return output
}

However a function can have one single output, which refers to exiting the function at that point ignoring what comes after:

function anyFunctionName(input) {
// do A
return output
// do B <-- this will never run due to being below the return-statement
}

It's also common to find functions that simply do something but don't return anything, e.g.:

function anyFunctionName(input) {
// do something
}

Types

TypeScript allows types in input and output so you can easily avoid giving or getting something wrong by mistake.

  • A function with 3 typed inputs:

    function anyFunctionName(input1: number, input2: string, input3: boolean) {
    // do something
    }
  • A function with typed output (e.g. as number):

    function anyFunctionName(): number {
    // do something
    return output
    }
    tip

    However if the output is clearly defined, you don't need to type the output in the function, but an automatic typing occurs. E.g.:

    function anyFunctionName() {
    return 3
    }

    In the Code Editor of DynaMaker, when you hover the mouse over a function like this, you will see that the output is automatically typed with a popup message like:

    function anyFunctionName(): number

export

Particularly in DynaMaker, you might have noticed the word export right before function in most functions. This allows the function to be used in other relevant tabs, without the need to have everything in one single tab.

One of the most common examples is that you have your functions that generate your sketches in GEOM2D, while your models (using these sketches) are in GEOM3D. Therefore, export is needed, e.g.:

GEOM2D
export function getRoundedRectangleSketch() {
const size = 50
const sketch = SKYCAD.generateRectangleSketch(0, 0, size, size, { r: 5 })
return sketch
}

so that you can you can use it in your model in GEOM3D:

GEOM3D
export function getPrismModel(extrudeLength: number) {
const model = new SKYCAD.ParametricModel()

const sketch = GEOM2D.getRoundedRectangleSketch()
const plane = new SKYCAD.Plane(0, 0, 1)

model.addExtrude(sketch, plane, extrudeLength)

return model
}

Examples

These are the most common examples of functions used in DynaMaker:

  • function to generate a model with 3 inputs:

    function generateCubeModel(width: number, depth: number, height: number) {
    const model = new SKYCAD.ParametricModel()

    const sketch = SKYCAD.generateRectangleSketch(0, 0, width, depth)
    const plane = new SKYCAD.Plane(0, 0, 1)

    model.addExtrude(sketch, plane, height)

    return model
    }
  • function to simply get some calculation:

    function getNrPads(nrBeams: number) {
    const nrPadsPerBeam = 2
    const totalPads = nrBeams * padsPerBeam
    return totalPads
    }
  • function with a SKYMATH.Vector3D as input and output as number:

    function getArea(porchSize: SKYMATH.Vector3D) {
    const areaInMm = porchSize.x * porchSize.y // [mm]
    const areaInMetres = areaInMm * 1e-6 // [m²]
    return areaInMetres
    }
tip

Notice that a good name for the function can indicate clearly what it does. Poor or vague names can lead to misunderstandings and can make the later bug-hunting a tough task to work with.