# When can you use cos and sin outside of setup or draw

**URL:** <https://discourse.processing.org/t/when-can-you-use-cos-and-sin-outside-of-setup-or-draw/11805>\
**Category:** Beginners\
**Created:** [June 3, 2019, 5:29pm UTC](https://discourse.processing.org/t/when-can-you-use-cos-and-sin-outside-of-setup-or-draw/11805 "2019-06-03T17:29:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 3, 2019, 5:29pm UTC](https://discourse.processing.org/t/when-can-you-use-cos-and-sin-outside-of-setup-or-draw/11805/1 "2019-06-03T17:29:07Z")

</div>

Is there any instances where you can do this. So far I understand that you can create a function outside of setup or draw which contain cos or sin, but they have to be called in draw or setup to work or it will give you an error. Also wanted to check if there is any way around this.

Thanks.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 3, 2019, 5:41pm UTC](https://discourse.processing.org/t/when-can-you-use-cos-and-sin-outside-of-setup-or-draw/11805/2 "2019-06-03T17:41:53Z")

</div>

All Processing’s (PApplet) `static` members can be accessed from anywhere: 🌩

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 3, 2019, 5:57pm UTC](https://discourse.processing.org/t/when-can-you-use-cos-and-sin-outside-of-setup-or-draw/11805/3 "2019-06-03T17:57:52Z")

</div>

Not sure what you mean by PAplet, (fairly new to programming). I’m currently using p5.js, I would assume perhaps wrongly that processing uses the same logical structure. But just to clarify so far I haven’t been able to use sin, cos, outside of setup or draw, other functions such as round or random are also unavailable.

Please note that it doesnt not mean that they can’t be written outside of setup or draw, but they have to be processed through setup or draw.

ie

this will not work;

```auto

var a = cos(60)*100;
var b;
function setup(){

function someFunction(){

b = a*200;
}
}

```

nor this;

```auto
var b;
function setup(){

function someFunction(){

var a = cos(60)*100;
return a;
}
}

var b = someFunction();

function draw(){
};

```

However this will work;

```auto
var b;
function setup(){

}

function someFunction(){

var a = cos(60)*100;
b = a*200;

return b;
}

function draw(){
var a = someFunction();
};

```

and this will work;

```auto
var b;
function setup(){

function someFunction(){

var a = cos(60)*100;
return a;
}
}

function draw(){
var b = someFunction();
};

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 3, 2019, 6:19pm UTC](https://discourse.processing.org/t/when-can-you-use-cos-and-sin-outside-of-setup-or-draw/11805/4 "2019-06-03T18:19:02Z")

</div>

> [@paulgoux](#):
>
> I’m currently using p5.js,

I’ve changed your post to the p5.js forum category then. 😑

p5js is a diff. flavor beast. 🐻

Indeed, none of its API is available until either it finds out that **setup()** or **draw()** is in the global context (_window_) or we manually instantiate class p5: `new p5;` 🤠

```auto
new p5; // prematurely instantiate class p5. ;-)

const SQRT_3 = sqrt(3); // we can access p5's sqrt() method outside functions!

function setup() {
	print(SQRT_3);
}

```

For **cos()** & **sin()** and similar, we can always directly rely on the class Math: 💡

> **[Math - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math)**
>
> The Math namespace object contains static properties and methods for mathematical constants and functions.

```auto
const SQRT_3 = Math.sqrt(3); // Directly accessing JS' Math method sqrt().

function setup() {
	print(SQRT_3);
}

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [June 3, 2019, 6:22pm UTC](https://discourse.processing.org/t/when-can-you-use-cos-and-sin-outside-of-setup-or-draw/11805/5 "2019-06-03T18:22:42Z")

</div>

Ah ok, thanks for taking the time to answer.
