# OnEvent Student defined procedures in loops

**URL:** <https://forum.code.org/t/onevent-student-defined-procedures-in-loops/37202>\
**Category:** CS Principles\
**Tags:** applab, ap-exam, csd-unit-3\
**Created:** [November 7, 2022, 9:44pm UTC](https://forum.code.org/t/onevent-student-defined-procedures-in-loops/37202 "2022-11-07T21:44:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greiber](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/greiber/32/6369_2.png) [@greiber](https://forum.code.org/u/greiber)\
**Post date:** [November 7, 2022, 9:44pm UTC](https://forum.code.org/t/onevent-student-defined-procedures-in-loops/37202/1 "2022-11-07T21:44:55Z")

</div>

AP question 3d on the app project asks for multiple calls of a student defined procedure that uses different parameter values to produce different results.

If the procedure is called from within a loop, what is the best way to explain to the college board that the same line of code makes different calls?

Similarly, if the procedure is called as part of an event, it is again the same single line of code calling the procedure, even though the differing events result in different parameter values.  
…Is there a right way to word that?

I would rather not have my students shy away from using procedures in loops or calling them from within events as a means of reverse engineering an easier answer to the test question.

---

<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:** [November 7, 2022, 10:14pm UTC](https://forum.code.org/t/onevent-student-defined-procedures-in-loops/37202/2 "2022-11-07T22:14:26Z")

</div>

I would ask them to use specific examples from the values that the variables may have within the loop and demonstrate how different lines of code within the procedure are being executed with those different values.

---

<div class="post-metadata">

**Author:** ![greiber](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/greiber/32/6369_2.png) [@greiber](https://forum.code.org/u/greiber)\
**Post date:** [November 8, 2022, 6:07pm UTC](https://forum.code.org/t/onevent-student-defined-procedures-in-loops/37202/3 "2022-11-08T18:07:29Z")

</div>

Can you give me an example?  
My own example that I am worried does not work, looks something like this:

```auto
var myButtons = ["red", "green", "blue"];
for(var i = 0; i < myButtons.length i++){
      onEvent(myButtons[i], "click", processButton);
}

```

---

<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:** [November 8, 2022, 6:58pm UTC](https://forum.code.org/t/onevent-student-defined-procedures-in-loops/37202/4 "2022-11-08T18:58:07Z")

</div>

Just a simple example below:

```
var myButtons = ["red", "green", "blue", "white", "purple"];
for(var i = 0; i < myButtons.length i++){
   printMessage(myButtons[i]);
}

function printMessage(button) {
  if (button == "red" || button == "green" || button == "blue")
      console.log("Primary color");
  else
      console.log("Not a primary color"):
}
```

---

<div class="post-metadata">

**Author:** ![greiber](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/greiber/32/6369_2.png) [@greiber](https://forum.code.org/u/greiber)\
**Post date:** [November 8, 2022, 10:13pm UTC](https://forum.code.org/t/onevent-student-defined-procedures-in-loops/37202/5 "2022-11-08T22:13:22Z")

</div>

That’s all well and good, but no longer taking user input to choose the basis for the conditional check.

…but sticking with your example, “printMessage(myButtons[i]);” is still a single line of code that happens to be called 5 times.

It is OK that the 2 calls comes from 1 line?

> Describes _two calls_ to the procedure identified in written response  
> Each call must pass a different argument(s) that causes a  
> different segment of code in the algorithm to execute.  
> First call: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Second call: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

From my example

```auto
onEvent(myButtons[i], "click", processButton);

```

that line of code isn’t actually the call to processButton, but a parameter later called by the onEvent. I am wondering if it is possible to answer the AP test question using a callback.
