# Defining functions with an event listener inside?! Does this follow good programming practices?!

**URL:** <https://forum.code.org/t/defining-functions-with-an-event-listener-inside-does-this-follow-good-programming-practices/37058>\
**Category:** Coding and Debugging Help\
**Created:** [October 14, 2022, 1:38am UTC](https://forum.code.org/t/defining-functions-with-an-event-listener-inside-does-this-follow-good-programming-practices/37058 "2022-10-14T01:38:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![knizio](https://avatars.discourse-cdn.com/v4/letter/k/6bbea6/32.png) [@knizio](https://forum.code.org/u/knizio)\
**Post date:** [October 14, 2022, 1:38am UTC](https://forum.code.org/t/defining-functions-with-an-event-listener-inside-does-this-follow-good-programming-practices/37058/1 "2022-10-14T01:38:11Z")

</div>

This year, I’ve had a number of students come up with a clever solution for dealing with certain buttons that need to do the same things over and over. What they’ve done is defined a function that took parameters, and then called this function with the parameters necessary to create onEvents (event listeners)

As you can see in the following project, the student created a buttonChangeScreen function line 109 simply to switch screens. The function is called on lines 23, 24, 25, then again on 42, 43.

[[App Lab - Code.org](https://studio.code.org/projects/applab/OeKiHkxcwgGQFMr7ENGbT5d97D6OdA3GTH_MviXE0Mc)]

The answers to the quiz are: Q1) Carbon, Q2) Sulfur

My question is - is this good programming practice?! It doesn’t seem like it’s really “event driven” programming… it’s more like sequential programming before anything else!

I’m a little behind on looking at my formative assessments, and students are working on a bigger summative project now. Not sure whether to let this fly.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bhatnagars](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/bhatnagars/32/4919_2.png) [@bhatnagars](https://forum.code.org/u/bhatnagars)\
**Post date:** [October 14, 2022, 3:39am UTC](https://forum.code.org/t/defining-functions-with-an-event-listener-inside-does-this-follow-good-programming-practices/37058/2 "2022-10-14T03:39:10Z")

</div>

In my opinion, onEvents should be independent and not be placed inside functions since onEvents are triggered by events and functions may be called within onEvents or outside of onEvents.

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [October 16, 2022, 7:44pm UTC](https://forum.code.org/t/defining-functions-with-an-event-listener-inside-does-this-follow-good-programming-practices/37058/3 "2022-10-16T19:44:24Z")

</div>

Yes, this is fine. There is no danger of going rogue the way it is used. They have added an additional abstraction to the already abstracted `onEvent()`.

Fortunately, App Lab is based on Javascript.

```javascript
function buttonChangeScreen(button,Screen){
  onEvent(button,"click",function(){
    setScreen(Screen);
  });
}

```

The innermost function definition creates something called a closure. It is an advanced concept, but here it makes the whole thing work correctly. See also [Programming Concept onEvent](https://forum.code.org/t/programming-concept-onevent/32300), [Functions and Event handlers](https://forum.code.org/t/functions-and-event-handlers/15418), [OnEvents as Functions then Functions inside of Functions](https://forum.code.org/t/onevents-as-functions-then-functions-inside-of-functions/6906). If your students have found this it is time to talk about closures.
