# In function 'void loop()' - cannot be used as a function

**URL:** <https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184>\
**Category:** Programming\
**Created:** [December 4, 2022, 2:11am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184 "2022-12-04T02:11:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![twosun](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@twosun](https://forum.arduino.cc/u/twosun)\
**Post date:** [December 4, 2022, 2:11am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/1 "2022-12-04T02:11:42Z")

</div>

I'm working on modifying an Xbox one controller using an ATTiny85 Microchip.  
I'm trying to create a rapid fire mod - for testing purposes only. It was a project I had started earlier in the year meant to build a portfolio of completed projects. And finally decided to jump back on it recently.

Anyways, in my code I've ran into an issue inside the void loop(); section.  
I wrote an if statement that would detect if both joysticks are pressed at the same time, then the conditions would be met and call a function called fire\_mode() just outside the void loop(); with an argument being passed into the parameter.

after verifying the code, I received this error in question - "In function 'void loop()':  
rapid\_fire\_attiny85\_xbox\_one\_s:37:23: error: 'fire\_mode' cannot be used as a function"

Here's the entire code:

```cpp
int left_stick = 2;
int right_stick = 3;
int trigger = 4;
bool active = false;

void setup()
{
  pinMode(left_stick, INPUT);
  pinMode(trigger, INPUT);
  pinMode(right_stick, INPUT);
}
bool fire_mode(active_mode)
{
  if (active_mode == true)
  {
    if (digitalRead(trigger) >= 5)
    {
      pinMode(trigger, OUTPUT);
      digitalWrite(trigger, 1000);
      delay(22);
      pinMode(trigger, INPUT);
      delay(100);
    }
  }
  if (analogRead(left_stick) < 50 && analogRead(right_stick) < 50)
  {
    return bool active_mode = false;
  }
}
void loop()
{
  if (analogRead(left_stick) < 50 && analogRead(right_stick) < 50)
  {
    bool active = true;
    if (active == true)
    {
      fire_mode(active);
    }
  }
}

```

_Any ideas on what I'm doing wrong here?_

Cheers!

---

<div class="post-metadata">

**Author:** ![anon85221860](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon85221860](https://forum.arduino.cc/u/anon85221860)\
**Post date:** [December 4, 2022, 2:14am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/2 "2022-12-04T02:14:15Z")

</div>

> [@twosun](#):
>
> `bool fire_mode(active_mode)`

What's all this then?

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [December 4, 2022, 2:19am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/3 "2022-12-04T02:19:39Z")

</div>

> [@twosun](#):
>
> `bool fire_mode(active_mode)`

Missing argument type. Do you mean  
`void fire_mode(bool active_mode)`

---

<div class="post-metadata">

**Author:** ![twosun](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@twosun](https://forum.arduino.cc/u/twosun)\
**Post date:** [December 4, 2022, 2:57am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/4 "2022-12-04T02:57:01Z")

</div>

it's supposed to be the function declaration following the block of code.

I'm not sure if the syntax is correct, I'm actually coming from Python so it's been a processing transitioning.

---

<div class="post-metadata">

**Author:** ![twosun](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@twosun](https://forum.arduino.cc/u/twosun)\
**Post date:** [December 4, 2022, 3:06am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/5 "2022-12-04T03:06:58Z")

</div>

Okay, I corrected the argument type, and it cleared the void loop error.

So I made the following changes..

`fire_mode(active);` to `bool fire_mode(active);`

&

`bool fire_mode(active_mode)` to `bool fire_mode(bool active_mode)`

after verifying it, it cleared all the errors!

C++ has been getting me with all these syntax errors! LOL

---

<div class="post-metadata">

**Author:** ![anon85221860](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon85221860](https://forum.arduino.cc/u/anon85221860)\
**Post date:** [December 4, 2022, 3:08am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/6 "2022-12-04T03:08:44Z")

</div>

> [@twosun](#):
>
> So I made the following changes..
> 
> `fire_mode(active);` to `bool fire_mode(active);`

fire\_mode(true);  
**is** the way to make the function call

a bool / boolean is 'true' or 'false'

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [December 4, 2022, 3:13am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/7 "2022-12-04T03:13:03Z")

</div>

This:

> [@twosun](#):
>
> ```arduino
> bool active = true;
> if (active == true)
> {
> fire_mode(active);
> }
> 
> ```

is functionally identical to this:

```arduino
   fire_mode(active);

```

since "active" will ALWAYS be true when the if statement is encountered.

---

<div class="post-metadata">

**Author:** ![twosun](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@twosun](https://forum.arduino.cc/u/twosun)\
**Post date:** [December 4, 2022, 3:32am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/8 "2022-12-04T03:32:14Z")

</div>

Okay, I see what you're getting at.

So if I am understanding you correctly. You're saying it's not necessary to declare the variable type when calling a function?

so I should just be able to call the function name, pass in my argument, and close it off with a semi-colon.

function(arg);

---

<div class="post-metadata">

**Author:** ![twosun](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@twosun](https://forum.arduino.cc/u/twosun)\
**Post date:** [December 4, 2022, 3:37am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/9 "2022-12-04T03:37:02Z")

</div>

I see.  
So you're saying it's redundant to include the if-statement following active = true;

it would make more sense to just call the function right after the boolean variable.

I realized I've been following a process of creating if statements to ward off any undesirable affects in my code, not realizing at the same time I was creating unnecessary obstacles.

---

<div class="post-metadata">

**Author:** ![anon85221860](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anon85221860](https://forum.arduino.cc/u/anon85221860)\
**Post date:** [December 4, 2022, 3:45am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/10 "2022-12-04T03:45:04Z")

</div>

On reflection, Livingston is most correct.  
If I were passing a numeric variable then it would be bad form to use the number itself. So, likewise, 'best practice', you probably should send that variable alias instead of a 'raw' true/false.  
(But in the case of a bool, it seems like a lot more typing to go to.)

---

<div class="post-metadata">

**Author:** ![twosun](https://avatars.discourse-cdn.com/v4/letter/t/46a35a/32.png) [@twosun](https://forum.arduino.cc/u/twosun)\
**Post date:** [December 4, 2022, 3:54am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/11 "2022-12-04T03:54:21Z")

</div>

I agree, I had created the boolean variable with the intention of mutating it throughout the code. That way, under certain conditions certain blocks of code (i.e. the fire\_mode function) wouldn't turn on unintentionally.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [December 4, 2022, 10:49am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/12 "2022-12-04T10:49:47Z")

</div>

> [@twosun](#):
>
> `bool fire_mode(active_mode)` to `bool fire_mode(bool active_mode)`
> 
> after verifying it, it cleared all the errors!

It still is incorrect. If you also enable **warnings** then you'll find that your function does not return a value. If you declare a `bool fn()` then it should return a bool value. For no return value the correct function type is `void fn()`.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 2, 2023, 10:49am UTC](https://forum.arduino.cc/t/in-function-void-loop-cannot-be-used-as-a-function/1061184/13 "2023-06-02T10:49:48Z")

</div>

Closed on June 2, 2023, 10:49am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
