# Custom function not defined when called from a different routine

**URL:** https://discourse.psychopy.org/t/custom-function-not-defined-when-called-from-a-different-routine/13519
**Category:** Builder
**Tags:** pavlovia
**Created:** [May 20, 2020, 11:46am UTC](https://discourse.psychopy.org/t/custom-function-not-defined-when-called-from-a-different-routine/13519 "2020-05-20T11:46:41Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![niclai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/niclai/32/4405_2.png) [@niclai](https://discourse.psychopy.org/u/niclai)
#### Post date: [May 20, 2020, 11:46am UTC](https://discourse.psychopy.org/t/custom-function-not-defined-when-called-from-a-different-routine/13519/1 "2020-05-20T11:46:41Z")

</div>

When running the experiment from the builder in **Pavlovia** I get the following problem.

I define the following custom function using a code snippet at the beginning of the experiment. It just chooses a random number that I use later. `doJitterLine` is also defined at the beginning of the experiment.

```auto
function jitterLine(doJitterLine) {
    if (doJitterLine) {
        return [3,4,5,6,7][Math.floor(Math.random() * 5)] * (Math.round(Math.random()) * 2 - 1);
    } else {
        return 0;
    }

```

However, when I try to run it in a code snippet at the end of another routine I get:

` ***ReferenceError: jitterLine is not defined** `.

For now, I just copied the content of the function and pasted it in every routine I need to use it but which is not very optimal.

**Is it possible to define a JS function at the beginning of the experiment to reuse it in later routines? If so, what I’m missing in my function definition?**

Thanks!

---

<div class="post-metadata">

### Author: ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)
#### Post date: [May 20, 2020, 11:55am UTC](https://discourse.psychopy.org/t/custom-function-not-defined-when-called-from-a-different-routine/13519/2 "2020-05-20T11:55:51Z")

</div>

@niclai, you just need to assign your function to a variable name, so PsychoPy can declare the variable / function in global scope:

```auto
jitterLine = function(doJitterLine) { ... }

```

---

<div class="post-metadata">

### Author: ![niclai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/niclai/32/4405_2.png) [@niclai](https://discourse.psychopy.org/u/niclai)
#### Post date: [May 20, 2020, 12:05pm UTC](https://discourse.psychopy.org/t/custom-function-not-defined-when-called-from-a-different-routine/13519/3 "2020-05-20T12:05:05Z")

</div>

@dvbridges, yeah that sounds very important 😅  
That function definition was created by the python to JS translator of the builder. Is that a normal thing that function definitions are translated but not assigned to a variable?

---

<div class="post-metadata">

### Author: ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)
#### Post date: [May 20, 2020, 12:07pm UTC](https://discourse.psychopy.org/t/custom-function-not-defined-when-called-from-a-different-routine/13519/4 "2020-05-20T12:07:51Z")

</div>

Yes, its just the way the translation happens, but you could also just assign your function to a variable in Python and use the new name. This should translate correctly without the need to adapt the JS, e.g., in Python

```auto
def myFunction(vars):
    return True
newFunName = myFunction

```

---

<div class="post-metadata">

### Author: ![verdi](https://avatars.discourse-cdn.com/v4/letter/v/b38774/32.png) [@verdi](https://discourse.psychopy.org/u/verdi)
#### Post date: [May 9, 2022, 3:21pm UTC](https://discourse.psychopy.org/t/custom-function-not-defined-when-called-from-a-different-routine/13519/5 "2022-05-09T15:21:24Z")

</div>

Thanks for this, has anyone managed to get this to work? For some reason, it isn’t working for me. i.e. defining function in python, resetting value in python, using function in python in an ‘auto-\>JS’ code component and then trying to get it to work online…
