# Return Statement

**URL:** <https://forum.arduino.cc/t/return-statement/939670>\
**Category:** Programming\
**Created:** [December 26, 2021, 10:09pm UTC](https://forum.arduino.cc/t/return-statement/939670 "2021-12-26T22:09:16Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdrmachine](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bdrmachine/32/1156029_2.png) [@bdrmachine](https://forum.arduino.cc/u/bdrmachine)\
**Post date:** [December 26, 2021, 10:09pm UTC](https://forum.arduino.cc/t/return-statement/939670/1 "2021-12-26T22:09:16Z")

</div>

I am looking over some OneWire code and I do not totally understand the use of the return statements. Here is a example:

```cpp
void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit; 

  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }

```

The program at the point it reaches the return statement is not in a subroutine other than loop(). So where does it return to? Does it gist restart the loop() code?

---

<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:** [December 26, 2021, 10:14pm UTC](https://forum.arduino.cc/t/return-statement/939670/2 "2021-12-26T22:14:19Z")

</div>

It causes an exit from loop(). Since loop() is called repeatedly, the effect is to re-start the loop() code from where it begins.

---

<div class="post-metadata">

**Author:** ![bdrmachine](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bdrmachine/32/1156029_2.png) [@bdrmachine](https://forum.arduino.cc/u/bdrmachine)\
**Post date:** [December 26, 2021, 10:15pm UTC](https://forum.arduino.cc/t/return-statement/939670/3 "2021-12-26T22:15:45Z")

</div>

Thanks for clearing that up!

---

<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:** [December 26, 2021, 10:17pm UTC](https://forum.arduino.cc/t/return-statement/939670/4 "2021-12-26T22:17:49Z")

</div>

I never use that method. I would:

```cpp
void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit; 

  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
  }
  else
  { do the rest of the code in loop... }
}

```

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [December 26, 2021, 10:19pm UTC](https://forum.arduino.cc/t/return-statement/939670/5 "2021-12-26T22:19:51Z")

</div>

return there is to skip the code that follows that you failed to show.

---

<div class="post-metadata">

**Author:** ![bdrmachine](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bdrmachine/32/1156029_2.png) [@bdrmachine](https://forum.arduino.cc/u/bdrmachine)\
**Post date:** [December 26, 2021, 10:27pm UTC](https://forum.arduino.cc/t/return-statement/939670/6 "2021-12-26T22:27:15Z")

</div>

I consider myself to be a descent C programmer but Arduino does things a little different. I'm not sure why they ditched the main() sub to replace it with setup() and loop(). Thanks for the help.

---

<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:** [December 26, 2021, 10:34pm UTC](https://forum.arduino.cc/t/return-statement/939670/7 "2021-12-26T22:34:53Z")

</div>

I'm also at a loss. 🙂

It does save having to explain what 'while(1)' means.

---

<div class="post-metadata">

**Author:** ![oldcurmudgeon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oldcurmudgeon/32/1280882_2.png) [@oldcurmudgeon](https://forum.arduino.cc/u/oldcurmudgeon)\
**Post date:** [December 26, 2021, 11:18pm UTC](https://forum.arduino.cc/t/return-statement/939670/8 "2021-12-26T23:18:19Z")

</div>

When the Arduino was started decades ago it was intended as a simple way to use micro-controllers for non programmers. I thought of it as micros for artisans and poets. Since then it has grown like a cancer and become much more complex

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [December 26, 2021, 11:20pm UTC](https://forum.arduino.cc/t/return-statement/939670/9 "2021-12-26T23:20:03Z")

</div>

> [@bdrmachine](#):
>
> I'm not sure why they ditched the main() sub to replace it with setup() and loop().

There in fact is a main() **function** (they're not called subroutines in C / C++). It's just hidden from easy viewing (but you can find it) in the Arduino world. Its job is to initialize some hardware, call the setup() function where you do one-time stuff, and then call the loop() function inside an endless loop.

Now consider for a moment what your own main() function would do if this Arduino setup/loop paradigm didn't exist. It would initialize some hardware, do some one-time stuff, and then it would continually execute your main code in an endless loop. Sound familiar? So, the setup/loop paradigm works the way you'd write it yourself for 99.999% of the use cases.

---

<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:** [December 26, 2021, 11:22pm UTC](https://forum.arduino.cc/t/return-statement/939670/10 "2021-12-26T23:22:01Z")

</div>

Yeah, it's painless and does the same thing. I don't sweat it too much.

---

<div class="post-metadata">

**Author:** ![bdrmachine](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bdrmachine/32/1156029_2.png) [@bdrmachine](https://forum.arduino.cc/u/bdrmachine)\
**Post date:** [December 27, 2021, 2:37am UTC](https://forum.arduino.cc/t/return-statement/939670/11 "2021-12-27T02:37:12Z")

</div>

Don't leave us in the dark. How do you find the main function?

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [December 27, 2021, 4:04am UTC](https://forum.arduino.cc/t/return-statement/939670/12 "2021-12-27T04:04:09Z")

</div>

It depends on the details of your Arduino installation. I use [Portable Installations](https://www.arduino.cc/en/Guide/PortableIDE). So, for v1.8.15 of the IDE, 'main.cpp' is in 'D:\Arduino-1.8.15\hardware\arduino\avr\cores\arduino'

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [December 27, 2021, 10:29am UTC](https://forum.arduino.cc/t/return-statement/939670/13 "2021-12-27T10:29:52Z")

</div>

You are free to define your own main(), it will run instead

---

<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 25, 2022, 10:29am UTC](https://forum.arduino.cc/t/return-statement/939670/14 "2022-06-25T10:29:53Z")

</div>

Closed on June 25, 2022, 10:29am UTC

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