---
title: Modules
---

import Image from '@theme/IdealImage';

import pivot from '../../static/img/openscad-tutorial/pivot.png';

We can also define our own `module`s to associate some code with a name. Here's what it looks like.

```cpp
// highlight-start
module hingeBaseProfile() {
  translate([pivotRadius,0,0]){
    square([baseWidth,baseThickness]);
  }
}
// highlight-end

linear_extrude(hingeLength/2){
  offset(1)offset(-2)offset(1){
    translate([0,pivotRadius,0]){
      circle(pivotRadius);
    }
    square([pivotRadius,pivotRadius]);
    // highlight-next-line
    hingeBaseProfile(); // <- used here
  }
}
linear_extrude(hingeLength){
  // highlight-next-line
  offset(1)offset(-1)hingeBaseProfile(); // <- and here
}
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJx1kE2LwjAQhu/5FXPYQwOVfuEp28uePYgIHqRIbKY22E138yGC9L8ba5XIrrfJ+07eeWY+GlUWOSM/8tTbFRfSmXLOyJ4bXLeyPio0pizuwkYK25aZt1upDrhAdfDvImWEfPfCdQij/uVbl7pvZIcRhQsBsJor03GL0TaYE6dxWtGbD2B+Hdfefo6JXwgqynzbQAZCOqmQ6x2erXYCo4AkycewvmkM2iijUzHL6VO6Dwtw0vgF6IEDUEtde/7AHRFuECFv+Duoq6n57z0YJAl8zsAZFNCixmmv92v9v1RG32dzNUUPV0ZqnRE=)**

## Module Syntax

At the top is the module definition, the syntax here is `module yourName() { /* your code */ }` and then when we want to use it we put parenthesis `()` at the end i.e. `yourName();`.
The parenthesis are there because we can also pass arguments to modules which we'll cover soon!

Now that we know how to use modules, let's wrap everything we've done so far into one, as it's a good way to name and therefore express intent of the code we've written.

```cpp
// ... variables above
module hingeBaseProfile() {
  translate([pivotRadius,0,0]){
    square([baseWidth,baseThickness]);
  }
}

module hingeBodyHalf() {
  linear_extrude(hingeLength/2){
    offset(1)offset(-2)offset(1){
      translate([0,pivotRadius,0]){
        circle(pivotRadius);
      }
      square([pivotRadius,pivotRadius]);
      hingeBaseProfile();
    }
  }
  linear_extrude(hingeLength){
    offset(1)offset(-1)hingeBaseProfile();
  }
}

hingeBodyHalf();
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJx1UD1vwyAU3PkVDB2M5Cr+UCbkpVOGDFFVqUNlRcQ8YlQCKeCqVeX/XmI7Fk4bJMTj3cHdvQehq7Kg6Cw/jX9mXHauWlN0YA5eWtm8a3CuKsfGq+S+rfIAt1IfYQv6GO5lRhE6Gd4pwEP/KVB31gipICH4B2HsLdNOMQ/JW6STZmlWkwuOsfvomA3wLJMuHNSEBlqP+hslw783TIlJRkkNzO7hy9uOQxKZXBWTjhHCgU9yMhWPBZlbI2PhNksXfuuZg3EjbRMCRvjg8bL66byGiv+I6np+8HdsI9Kjcd8Pdi9WTv7/cxjhzezoL4pYpkI=)**

## 3D Primitives

Next lets work on the pin, ie. what the other half of the hinge will pivot about.
We're going to introduce a new module `cylinder` and to get in good habits lets put this immediately in a module that describes what we're making.

```cpp
// ... other variables above
pinRadius=2;
pinTaper=0.25;

// ... other module definitions above
// highlight-start
module pin() {
  translate([0,pivotRadius,hingeLength/2]){
    cylinder(h=hingeLength/2, r1=pinRadius, r2=pinRadius+pinTaper);
  }
}
// highlight-end

hingeBodyHalf();
pin();
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJx1UktPhDAQvvdX9OCBRlQe8UR68eTBgzGbeDDEVDosjVjWthg3hv9ugcK2+yAh7fSbx/fNzFUtaZ4VaCd+OvPCuOg1vS/QB9OwaUT1KUFrms8Pr4KbhqYWboTcwhPIrbXzZIyWLnZKJTdsB4omt5n1RV8d71vAU8yDTfOsulq0EBH8hzA2ikndMgPRm8chTuKkJCOOsf7umbLwSiEO2JWksG4DGo4qdXz/yNralWmFBKbe4deonkPkCbjLXJ2urjWYKCXucpOR9Wn2CNgmccC3XH0wroSqrEAPnziO3+DORZSfw7uXa8Bp22ZkQPN/WdglWSk5n9NvoZ3h6XxCxUELF/XV3vLhoKKGBniMVUrXLbFWdrCul4U5sDia4LRT9vgH5jHd2w==)**

<Image img={pivot} className="mb-8 bg-contain rounded-md overflow-hidden" />

A couple notes about the above.

- `cylinder` is the 3d version of `circle` when `h` for height is the length that we would need to extrude `circle` by. It can also take one or two radii, here we're using two because it allows us to add a taper to the pin. The reason why we want to add a taper is because we're starting to think about the assemble of this hinge and if we taper the pin it means the other half of the hinge will be locked on.
- In order to add the taper we've defined two variables `pinRadius` and `pitTaper`. The latter is extra we add to the second `cylinder` radius.
- The `translate` is there move the `cylinder` up so that it's centred with the hinge pivot and move along so that it's protruding out of the hinge pivot.
