# Uncaught TypeError: t is undefined (p5.min.js:4604:988), when trying to use button.mousePressed

**URL:** <https://discourse.processing.org/t/uncaught-typeerror-t-is-undefined-p5-min-js988-when-trying-to-use-button-mousepressed/32243>\
**Category:** Coding Questions\
**Created:** [September 13, 2021, 10:47pm UTC](https://discourse.processing.org/t/uncaught-typeerror-t-is-undefined-p5-min-js988-when-trying-to-use-button-mousepressed/32243 "2021-09-13T22:47:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kewyj](https://avatars.discourse-cdn.com/v4/letter/k/e495f1/32.png) [@kewyj](https://discourse.processing.org/u/kewyj)\
**Post date:** [September 13, 2021, 10:47pm UTC](https://discourse.processing.org/t/uncaught-typeerror-t-is-undefined-p5-min-js988-when-trying-to-use-button-mousepressed/32243/1 "2021-09-13T22:47:51Z")

</div>

I’m making a generator for rooms, and it takes in 3 arguments - number of rooms, min size, and max size of the rooms generated. The code works fine when I try to hard code it:

```auto
function setup() {      
    createCanvas(1280, 512);      
    //randomly creates x rooms of different sizes
    generator = new Room_map(5, 10, 15);
    //draw these rooms 
    generator.draw(); 
}

```

But when I try to take in user input using this example here [examples | p5.js](https://p5js.org/examples/dom-input-and-button.html), the above error shows up in the console.

Any help is apprieciated. Thanks in advance!

Here is my code for taking in user input:

```auto
function setup() {    
    createCanvas(1024, 512);

    input_no_of_rooms = createInput(); //no of rooms
    input_no_of_rooms.position(1100, 50);

    input_min_size = createInput(); //min size
    input_min_size.position(1100, 90);

    input_max_size = createInput(); //max size
    input_max_size.position(1100, 130);

    button = createButton('Generate');
    button.position(1100, 155);

    //when button is pressed, call gen function with the above inputs
    button.mousePressed(gen(input_no_of_rooms.value(), 
                            input_min_size.value(), 
                            input_max_size.value()));   
}

function gen(no, min, max)
{
    if (no > 0 && min > 0 && max > 0)
    {
        console.log("generating"); //does not show up in log after pressing button

        //randomly creates x rooms of different sizes
        var generation = new Room_map(no, min, max);

        //draw these rooms 
        generation.draw();
    }
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [September 14, 2021, 9:47pm UTC](https://discourse.processing.org/t/uncaught-typeerror-t-is-undefined-p5-min-js988-when-trying-to-use-button-mousepressed/32243/2 "2021-09-14T21:47:24Z")

</div>

Hi @kewyj,

Welcome to the forum! 😉

Remember to format your code by using the `</>` button when editing a message on the forum.

If you check the [`p5.Element.mousePressed()`](https://p5js.org/reference/#/p5.Element/mousePressed) reference, it says this:

- Syntax:  
`mousePressed(fxn)`

- Parameters

So it’s expecting either a function or a boolean but in your code you are actually providing the return value of your `gen` function.

Let me explain:

When the JS engine executes your code, it reads this:

```javascript
button.mousePressed(gen(input_no_of_rooms.value(), 
                        input_min_size.value(), 
                        input_max_size.value()));   

```

It’s a method call so it first evaluates the parameters which are:

```javascript
gen(input_no_of_rooms.value(), 
    input_min_size.value(), 
    input_max_size.value())

```

and this is a function call to the `gen` function so it runs that function and store its result. The thing is that your `gen` function doesn’t return anything so by default it’s [returning `undefined`](https://stackoverflow.com/questions/17337064/does-every-javascript-function-have-to-return-a-value).

And `undefined` is not a function! So when you press the button it fails to call that function.

So what is the solution here?

Two things:

- If your function wasn’t taking any arguments or you didn’t care about them, you could just put the name of the function like this:

- On the other side, if you need to pass some arguments you can either [`bind()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) the function with the parameters:

Have fun! 😋
