---
title: Module Arguments
---

import Image from '@theme/IdealImage';

import multiRotate from '../../static/img/openscad-tutorial/multi-rotate.png';
import bothHalves from '../../static/img/openscad-tutorial/both-halves.png';
import exaggeratedClearance from '../../static/img/openscad-tutorial/exaggerated-clearance.png';
import transparentAssembly from '../../static/img/openscad-tutorial/transparent-assembly.png';

Problem still stands that we now have two modules that are almost identical but not quiet, `pin2` was supposed to be temporary.
we can resolve this issue by using module arguments, lets start by comparing the two modules.

```cpp
module pin() {
  translate([0,pivotRadius,hingeHalfExtrudeLength+tiny]){
    cylinder(h=hingeLength/2+clearance/2, r1=pinRadius, r2=pinRadius+pinTaper);
  }
}

module pin2() {
  translate([0,pivotRadius,hingeHalfExtrudeLength+tiny]){
    rotate([0,180,0]){
      cylinder(h=hingeLength/2+clearance/2, r1=pinRadius, r2=pinRadius+pinTaper);
    }
  }
}
```

The only difference between the two is the `rotate` in `pin2`, further if we add a `rotate([0,0,0])` to `pin` it has no effect on the shape but it it does mean now the only difference between the two is the amount the pin is rotated.

```cpp
module pin() {
  translate([0,pivotRadius,hingeHalfExtrudeLength+tiny]){
    // highlight-next-line
    rotate([0,0,0]) {
      cylinder(h=hingeLength/2+clearance/2, r1=pinRadius, r2=pinRadius+pinTaper);
    }
  }
}

module pin2() {
  translate([0,pivotRadius,hingeHalfExtrudeLength+tiny]){
    rotate([0,180,0]){
      cylinder(h=hingeLength/2+clearance/2, r1=pinRadius, r2=pinRadius+pinTaper);
    }
  }
}
```

This is perfect for an argument! What's an argument? Its a value that we can pass into the module instead of having to define it ahead of time, this makes our module more flexible.
Here's how we'd modify `pin` to use an argument for the rotation:

```cpp
// highlight-next-line
module pin(rotateY) {
  translate([0,pivotRadius,hingeHalfExtrudeLength+tiny]){
    // highlight-next-line
    rotate([0,rotateY,0]) {
      cylinder(h=hingeLength/2+clearance/2, r1=pinRadius, r2=pinRadius+pinTaper);
    }
  }
}

pin(0);
pin(45);
pin(120);
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJxtkMFrwyAUxu/+FR52MCRdjVlPweNgh51GoYzRg4uvjdSaoma0jP7veyZpF8YEwe/p93vP72HnZCVqcjJfXXxT2vRBrmryqQKsW9McHIQgq7GwMTq2ssTr1rg9vILbo654crvJO6DcWp3AS/4o8G1jQXnlGkh6sr4ou3s+R9/rG2VGXIrF3bNERzTugl7OEUaOne4tUOzBfBdVhPeMfhNKIz4PFjX74MXsM8X//fIE3WbJSekISsYJWfDtSE2ruVjjNHj2Z8h8NmRBfSnvIaASvyq/5ZHVA/FK0r6SlBPj2ZAXe1pNh1KkEvkBqMmEYw==)**

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

Earlier I said that the syntax for modules was `module yourName() { /* your code */ }`, lets revise that to `module yourName(yourArguments) { /* your code that uses arguments */ }`.
As you can see we're using the argument `rotateY` within `rotate` so that that we can change `pin`'s orientation each time we use it by as demonstrated by using `0`, `45` and `120`.
An argument is much like a variable in that we should name it well, the difference is that it's passed to a module and can only be used within the module.
Here our code using our new module instead of `pin2`

```cpp
// ... rest of code above
module pin(rotateY) {
  translate([0,pivotRadius,hingeHalfExtrudeLength+tiny]){
    rotate([0,rotateY,0]) {
      cylinder(h=hingeLength/2+clearance/2, r1=pinRadius, r2=pinRadius+pinTaper);
    }
  }
}

difference() {
  hingeBodyHalf();
	// I wouldn't count 180 as a "magic number"
	// since it's easy to tell it's for a half turn
  pin(rotateY=180);
}
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJx9Ul1Lw0AQfDa/YilCE5raJEUQSl4EQcEHkYKIFLkmm/bwelfvLmqR/Hc3nya1GAi5vd2Z3ZnseSbjebRw9vxD2UeW8tzElwtnzQwutzx5k2hMPK8vnnhqt3FI6S2XG7xHuaF4HpRo2WArKrlke9RxcBFRbSKQaSYTLOMGestEdvNldZ62LD3GWTTtMDNCWC4PhA0CInN2Ks0FQlV+TTM9aJVxga4H3w6AJZARzKL70hPkB36w8so8gHnPmaZ0p8cfSF15CyornOKok0oP5cxNG8ElzfeKtQL3tKSmocoyg9YNveYwjbzuqq4YjB34g8FXXQ1AwjX54vby1bDlUzTfVl2fo3dedYC//tWZwqnfUwr/lxV6pzn7XtJmuFpZEvr8538NhZ+2dFKuQmtJTVQCG8rSLWjtSg4kIUXtHq3WpLdaPugw7laXoug3mrRbPDCGpKQ8y1AjETTLcLQgC+dsNoM7+FS5SOXYQqJyaSG8CoAZYDDasQ1PQOa7NepRVWw4sQG3YwPIzAGsAotC1DeZ0oTaEjfYXEtq2HMxJlrqWDg/WYc6OQ==)**

And now we can use the pin as both the hole and the shaft, above is code for the hole, and both together are below:

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

We're still not done with `pin` though! one more thing. Technically we can use the same module `pin` for the hole and shaft, but practically we can't because we haven't added any clearance between the two.
If we tried to print these together they would print solid.
We can fix this with another argument that makes the hole a bit larger:

```cpp
// highlight-next-line
module pin(rotateY, radiusOffset) {
  translate([0,pivotRadius,hingeHalfExtrudeLength+tiny]){
    rotate([0,rotateY,0]) {
      cylinder(
        h=hingeLength/2+clearance/2,
        // highlight-start
        r1=pinRadius+radiusOffset,
        r2=pinRadius+pinTaper+radiusOffset
        // highlight-end
      );
    }
  }
}
```

The new argument allows us to increase the radius of the pin, here's an exaggerated example:

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

That's too much clearance just to demonstrate the principle, in reality we'll use the `clearance` variable that we've already defined with a value of `0.2`. We're ready to put the hinge together now, lets make two modules `hingeHalfMale` and `hingeHalfFemale` which will be assemblies of modules we've already made.

```cpp
// ... previous module definitions above
module hingeHalfFemale() {
  difference() {
    hingeBodyHalf();
    // highlight-next-line
    pin(rotateY=180, radiusOffset=clearance);
  }
}

module hingeHalfMale() {
  translate([0,0,hingeLength]) {
    rotate([0,180,0]) {
      hingeBodyHalf();
      // highlight-next-line
      pin(rotateY=0, radiusOffset=0);
    }
  }
}

%hingeHalfFemale(); // make female hinge transparent
hingeHalfMale();
```
**[Live Demo](https://cadhub.xyz/dev-ide/openscad#encoded_script_v2=eJx9U9FKwzAUfe9X5EGhZZ1LOwSh9EVQfFAUGYjIkNjebmFdOtNMHLJ/96ZpY9oVB2Vt7rn3nnN6elaIdB4n3o5/VeqZ5Xxfp5eJ98FqWKx5thFQ1+ncHLzwXK3TCMtrLlZwD2KFz3Oqu0Xb24wSC7YDmdKLGLFZCUwykYF+blvvWFncfCu5z7spzsRZPLU9M+xQXBywl1Ic5m2rfF8CaeDXyOlJVgUvwQ/Ij0eIwqa6ZAr8N0dQSEO6DHSdkPpzzySWrZ6wJ3UZJAg7esfBpio/aM7tmpIL5PcORoE/LqldWBVFDcqPgvZmGgf2yCB6tGnYI760GEIyLtEX36k3ZPXv2P536twZzv3SNpz6ZypHz1xjCv+XFQXjM10vMRm+rBQKfQ2JbCg9Nu0nb69vw7jBEx2MziAzVjd2C9A70pmXHVBQDtK3Zg4SN3ESF1qQjFKb7InL14HEDqRLfg/bQnsGD+Klpd3Cltkc57woQALS8TsRgxyaaY6haXRF+6amVtN4qPWgBzb27eD3Ejr2WCP/TNbLXINH2fX5DdnRoSPnJ14kZDYjW7YBUjQnZo1husOgC+UNlCS/dQCJfw==)**

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

Well done, it's really coming together (I made the female hinge transparent so we can see how it fits together).
The only thing left to do is add mounting holes on the plates.
