# Loop in a Function?

**URL:** <https://forum.arduino.cc/t/loop-in-a-function/1144354>\
**Category:** Programming\
**Created:** [July 3, 2023, 10:44pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354 "2023-07-03T22:44:38Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![csgaraglino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/csgaraglino/32/942933_2.png) [@csgaraglino](https://forum.arduino.cc/u/csgaraglino)\
**Post date:** [July 3, 2023, 10:44pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/1 "2023-07-03T22:44:39Z")

</div>

I am stuck on how to simply loop code inside of a Function.

I am trying to:

```arduino
void onError(){
   void loop {
      digitalWrite(LED_R, LOW);
      delay(1000);  
      digitalWrite(LED_R, HIGH);
      delay(1000);
   }
}

```

I know this does not work, But I need a simple way to accomplish the same result.  
In a large sketch, all I want to do is blink an LED if an error is present.

In this case, if an SD Card is not in the reader, my code knows and calls onError. I can light the LED, but I can't get it to blink.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 3, 2023, 10:49pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/2 "2023-07-03T22:49:05Z")

</div>

```arduino
void onError(){
   while(1) {
      digitalWrite(LED_R, LOW);
      delay(1000);  
      digitalWrite(LED_R, HIGH);
      delay(1000);
   }
}

```

When you say,

```arduino
   void loop {
..
   }

```

You are **declaring** , i.e. defining, a function, not **calling** it.  
You can not declare a function inside another function.

The predefined 'loop()' function **only** repeats because the core software calls it repeatedly from inside a 'while' statement block. It is not part of the C/C++ language.

---

<div class="post-metadata">

**Author:** ![csgaraglino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/csgaraglino/32/942933_2.png) [@csgaraglino](https://forum.arduino.cc/u/csgaraglino)\
**Post date:** [July 3, 2023, 11:00pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/3 "2023-07-03T23:00:49Z")

</div>

> [@anon57585045](#):
>
> ```arduino
> while(1) {
> digitalWrite(LED_R, LOW);
> delay(1000);  
> digitalWrite(LED_R, HIGH);
> delay(1000);
> }
> 
> ```

Thanks!

I tried

```arduino
 while(i<10) {
      digitalWrite(LED_R, LOW);
      delay(1000);  
      digitalWrite(LED_R, HIGH);
      delay(1000);
      i++;

```

and it kept telling me I had n error.

But yours worked, again, thank you!

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 3, 2023, 11:57pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/4 "2023-07-03T23:57:52Z")

</div>

All's well that ends well, but if you have errors, please cut and paste them verbatim, rather than just mentioning or paraphrasing them. If you wanted to do the loop10 times you could do that too, but your example is chopped off so I can't see what you did.

I would venture to guess, you didn't declare the variable 'i'.

```arduino
void onError(){
int i = 0;
while(i<10) {
      digitalWrite(LED_R, LOW);
      delay(1000);  
      digitalWrite(LED_R, HIGH);
      delay(1000);
      i++;
  }
}

```

---

<div class="post-metadata">

**Author:** ![csgaraglino](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/csgaraglino/32/942933_2.png) [@csgaraglino](https://forum.arduino.cc/u/csgaraglino)\
**Post date:** [July 4, 2023, 1:36pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/5 "2023-07-04T13:36:28Z")

</div>

No you did exactly what I was trying to accomplish, thank you. I am new to this language and tried a dozen different options. I didn’t past all those errors because I didn’t know what was even the right path.

I knew my example was incorrect, but it visually represented what I was trying to do.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 4, 2023, 2:08pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/6 "2023-07-04T14:08:31Z")

</div>

you can use one of the [iteration statements](https://en.cppreference.com/w/cpp/language/statements)

if you want an infinite loop:

```arduino
while (true) {
  // your repeating code goes here
}

```

or

```arduino
for (;;) {
  // your repeating code goes here
}

```

if you want to repeat just a certain number of time, the for loop makes it easy

```arduino
for (size_t n = 0; n < 100; n++) {
  // your repeating code goes here
  // n will vary from 0 to 99 (so the loop is executed 100 times)
}

```

size\_t is a type that is suitable for counting or array indexes etc. It's unsigned so if you need to variable to go from -42 to 58 then use a different type like `int` or `long`

```arduino
for (int n = -42; n < 58; n++) {
  // your repeating code goes here
  // n will vary from -42 to 57 (so the loop is executed 100 times)
}

```

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [July 4, 2023, 2:32pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/7 "2023-07-04T14:32:31Z")

</div>

if you need a blinking led, Arduino has prepared the example "Blink Without Delay"

> **[Blink Without Delay | Arduino Documentation](https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay)**
>
> Blink an LED without using the delay() function.

You can even find that example in your IDE examples.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 4, 2023, 5:14pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/8 "2023-07-04T17:14:56Z")

</div>

> [@noiasca](#):
>
> if you need a blinking led, Arduino has prepared the example "Blink Without Delay"

My understanding was that he needed the code to be stuck in the blinking loop. (Sure you could use that as well but the basic blink would do)

> [@csgaraglino](#):
>
> sketch, all I want to do is blink an LED if an error is present.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 4, 2023, 5:28pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/9 "2023-07-04T17:28:04Z")

</div>

> [@anon57585045](#):
>
> ` while(1) {`

Why not --  
`while(true) {`

---

<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:** [December 31, 2023, 5:28pm UTC](https://forum.arduino.cc/t/loop-in-a-function/1144354/10 "2023-12-31T17:28:38Z")

</div>

Closed on December 31, 2023, 5:28pm UTC

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