# Custom functions in Pavlovia

**URL:** <https://discourse.psychopy.org/t/custom-functions-in-pavlovia/29060>\
**Category:** Online experiments\
**Created:** [May 9, 2022, 2:56pm UTC](https://discourse.psychopy.org/t/custom-functions-in-pavlovia/29060 "2022-05-09T14:56:16Z")\
**Posts on this page:** 6\
**Page:** 1

<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, 2:56pm UTC](https://discourse.psychopy.org/t/custom-functions-in-pavlovia/29060/1 "2022-05-09T14:56:17Z")

</div>

Hi,

I’m trying to custom define a function which I then use in Python in an auto-\>JS code component. However, I’m getting the ‘reference error: can’t find variable x’ for the function name. I’ve seen [here](https://docs.google.com/document/d/183xmwDgSbnJZHMGf3yWpieV9Bx8y7fOCm3QKkMOOXFQ/edit#heading=h.s6ere122j2ek) that I should define it in my JS code block. However, this isn’t working for me. Are there any ideas about how I could do this?

Many thanks 🙂

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [May 9, 2022, 3:19pm UTC](https://discourse.psychopy.org/t/custom-functions-in-pavlovia/29060/2 "2022-05-09T15:19:39Z")

</div>

How have you worded the function definition?

Is it in Before or Begin Experiment?

> [@JS function not recognised](https://discourse.psychopy.org/t/js-function-not-recognised/29054/2):
>
> Try putting your function definitions in Before Experiment or assigning them to a variable name varInstance = function(variability, scaling, shift)

---

<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:24pm UTC](https://discourse.psychopy.org/t/custom-functions-in-pavlovia/29060/3 "2022-05-09T15:24:33Z")

</div>

It is in before experiment. i.e.:

 ![Screenshot 2022-05-09 at 16.23.29](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/7/d/7d0e1301e7e667e93d3f3344495afce04b51532c.jpeg)  
and then I try to use it in a later code component here:  
 ![Screenshot 2022-05-09 at 16.24.13](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/f/c/fc29f961bac6bf799ccaa1c6f599e1b7461e6681.jpeg)  
but even though it is defined in the previous code block, it claims it isn’t recognised…

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [May 9, 2022, 4:09pm UTC](https://discourse.psychopy.org/t/custom-functions-in-pavlovia/29060/4 "2022-05-09T16:09:09Z")

</div>

The syntax you are using Before Experiment is the version I use in Begin Experiment.

In Before Experiment I tend to write the function in Python. Try moving your code to Begin Experiment.

---

<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 11, 2022, 2:18pm UTC](https://discourse.psychopy.org/t/custom-functions-in-pavlovia/29060/5 "2022-05-11T14:18:16Z")

</div>

Thank you. When you write the function in Python, how do you then make sure that it translates this to JS so you can use it in Pavlovia?

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [May 12, 2022, 10:51am UTC](https://discourse.psychopy.org/t/custom-functions-in-pavlovia/29060/6 "2022-05-12T10:51:04Z")

</div>

Start by trying something in this format in an Auto Translate component.

```auto
def functionName(x):
     do stuff
     return functionOutput

```

If it doesn’t translate automatically you need to switch the component to Both to edit the JavaScript.
