# Code explanation needed please

**URL:** <https://forum.arduino.cc/t/code-explanation-needed-please/920988>\
**Category:** Programming\
**Created:** [November 2, 2021, 9:17am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988 "2021-11-02T09:17:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon77222424](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon77222424](https://forum.arduino.cc/u/anon77222424)\
**Post date:** [November 2, 2021, 9:17am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/1 "2021-11-02T09:17:35Z")

</div>

Hi I searched but can not find an explanation on what this code block  
mean or what it does. Also why when I use this it seem to take a long time to execute and the program becomes very slow. There are three blocks like this in the sketch.

```cpp
int lookRight()
{
    myservo.write(0); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(90); 
    return distance;
}

```

This is not in the loop but **lookright()** is called in the loop.  
Could some help me understand this and what would it be called.

Regards

Henry

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 2, 2021, 9:22am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/2 "2021-11-02T09:22:55Z")

</div>

> [@anon77222424](#):
>
> ```arduino
> int lookRight()
> 
> ```
> 
> {

It's a function that returns a int value.

> [@anon77222424](#):
>
> ```arduino
> myservo.write(0);
> 
> ```

Send the servo to 0 degrees.

> [@anon77222424](#):
>
> ```arduino
> delay(500);
> 
> ```

Delay

> [@anon77222424](#):
>
> ```arduino
> int distance = readPing();
> 
> ```

Take a ping reading and store the ping readings into a variable named distance.

> [@anon77222424](#):
>
> ```arduino
> delay(100);
> 
> ```

Delay again, so far a total delay of .6 seconds. And he wonders why it takes so long.

> [@anon77222424](#):
>
> ```arduino
> myservo.write(90);
> 
> ```

Send the servo to 90 degrees.

> [@anon77222424](#):
>
> ```arduino
> return distance;
> 
> ```

return the distance.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 2, 2021, 9:47am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/3 "2021-11-02T09:47:09Z")

</div>

> [@anon77222424](#):
>
> There are three blocks like this in the sketch.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'  
  
Use **code tags** (the \</\> icon above the compose window)&nbsp;to make it easier to read and copy for examination

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

---

<div class="post-metadata">

**Author:** ![anon77222424](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon77222424](https://forum.arduino.cc/u/anon77222424)\
**Post date:** [November 2, 2021, 10:51am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/4 "2021-11-02T10:51:47Z")

</div>

Thanks for the explanation. Would there be a name for a code block like this?  
I used to program PIC's and there you would use "makro's". Is this the same thing.?  
I am new to the forum and can not upload yet.

I found a sketch that used these routines and added them to my own sketch for a Bluetooth tank that was working fine. I found that the lookright() and loofleft() routine did not do anything but the readping() did work. But suddenly if I gave the go forward command the tank would do nothing for 3 to 4 seconds and only then the wheels would start spinning. I then went and exchanged the call for lookriht() with the code in this routine and the same with lookleft() and readping(). I deleted the routines and now it is working fine. When I give forward command the tank is responsive again and the servo and HC-SR04 is working as expected.

---

<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:** [November 2, 2021, 10:58am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/5 "2021-11-02T10:58:14Z")

</div>

> [@anon77222424](#):
>
> Would there be a name for a code block like this?

"function" - first sentence in reply #2

---

<div class="post-metadata">

**Author:** ![anon77222424](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon77222424](https://forum.arduino.cc/u/anon77222424)\
**Post date:** [November 2, 2021, 11:14am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/6 "2021-11-02T11:14:26Z")

</div>

Thanks aarg,

Is this very processing intensive? Why would it have such a negative impact?

---

<div class="post-metadata">

**Author:** ![sionaggutraidh](https://avatars.discourse-cdn.com/v4/letter/s/a88e57/32.png) [@sionaggutraidh](https://forum.arduino.cc/u/sionaggutraidh)\
**Post date:** [November 2, 2021, 11:15am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/7 "2021-11-02T11:15:51Z")

</div>

> [@anon77222424](#):
>
> Why would it have such a negative impact?

There's 600ms worth of delay() in there...

---

<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:** [November 2, 2021, 11:16am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/8 "2021-11-02T11:16:31Z")

</div>

> [@anon77222424](#):
>
> Is this very processing intensive?

If you are referring to the delay() calls, effectively yes. They tie up the processor for long periods of time during which nothing happens. By definition...

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 2, 2021, 11:20am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/9 "2021-11-02T11:20:04Z")

</div>

> [@anon77222424](#):
>
> Is this very processing intensive? Why would it have such a negative impact?

Delay is a NOP instruction. Means, basically, do nothing. Its not processor intensive at all because the CPU is told to do NOTHING with a NOP; .6 seconds of doing nothing.

---

<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:** [November 2, 2021, 11:26am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/10 "2021-11-02T11:26:44Z")

</div>

> [@Idahowalker](#):
>
> Its not processor intensive at all because the CPU is told to do NOTHING

However, the end result is the same, from the point of view of other tasks that need to run. That is why I qualified my statement with, "effectively".

Obviously, all processors are equally good at doing nothing. 🙂

---

<div class="post-metadata">

**Author:** ![anon77222424](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon77222424](https://forum.arduino.cc/u/anon77222424)\
**Post date:** [November 2, 2021, 11:28am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/11 "2021-11-02T11:28:52Z")

</div>

Yes I know about the delays.  
I would imagine that this is only executed when it is called from the loop or is it executed the whole time.

I took the code inside "lookright() function" and replaced the the call for lookright() in the loop with exactly the same code as in the function and then deleted the function. Did the same with the other two functions and that solved the sluggishness.

---

<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:** [November 2, 2021, 11:30am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/12 "2021-11-02T11:30:36Z")

</div>

> [@anon77222424](#):
>
> I took the code inside "lookright() function" and replaced the the call for lookright() in the loop with exactly the same code as the in function and then deleted the function. Did the same with the other two functions and that solved the sluggishness.

A function call by itself, executes in only a few microseconds. So you clearly did something else that you're not aware of.

> I would imagine that this is only executed when it is called from the loop or is it executed the whole time.

Functions only execute when they are called. How else could you control when they execute?

---

<div class="post-metadata">

**Author:** ![anon77222424](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon77222424](https://forum.arduino.cc/u/anon77222424)\
**Post date:** [November 2, 2021, 11:37am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/13 "2021-11-02T11:37:33Z")

</div>

Thanks, I will try it again and if it is still a problem I will post the code.

Just for interest what would you call a block of code that starts with "void"

```cpp
void moveStop() {
  motor1.run(RELEASE); 
  motor2.run(RELEASE);
  } 

```

---

<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:** [November 2, 2021, 11:39am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/14 "2021-11-02T11:39:15Z")

</div>

A function returning a void datatype.

---

<div class="post-metadata">

**Author:** ![anon77222424](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon77222424](https://forum.arduino.cc/u/anon77222424)\
**Post date:** [November 2, 2021, 11:55am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/15 "2021-11-02T11:55:02Z")

</div>

Finally found a good explanation:

> **[Using Functions in a Sketch | Arduino Documentation](https://docs.arduino.cc/learn/programming/functions)**
>
> Learn how to define and use functions in a Sketch.

![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/d/6/0d6ab5ec7b2f3d74b93450c22658a1f7899647ca.png)

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [November 2, 2021, 11:57am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/16 "2021-11-02T11:57:19Z")

</div>

Dang, there is that word again, "Function".

---

<div class="post-metadata">

**Author:** ![anon77222424](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon77222424](https://forum.arduino.cc/u/anon77222424)\
**Post date:** [November 2, 2021, 12:01pm UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/17 "2021-11-02T12:01:13Z")

</div>

Yep I used to call them subroutines in pic programming

---

<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:** [November 2, 2021, 12:01pm UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/18 "2021-11-02T12:01:47Z")

</div>

Subroutines have no parameters or return values.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [November 3, 2021, 10:36am UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/19 "2021-11-03T10:36:50Z")

</div>

QBasic.

> When you want to execute MySub, code this:
> 
> CALL MySub
> 
> And the subroutine will be executed. In languages like QBasic/QuickBasic  
> which have 'variable scoping', subroutines, by default, have their own  
> 'local' variables. This is good, because it lets you code the subroutine  
> without being concerned about trashing a variable of the same name used  
> outside the subroutine. There are two mechanisms for the subroutine to  
> communicate with the rest of the program. **One is by passing 'parameters' to**  
> **the subroutine when you call it. These parameters are defined in the**  
> **subroutine definition like this:**
> 
> SUB MySub( **P1,P2** )  
>   
> END SUB

Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [November 3, 2021, 6:42pm UTC](https://forum.arduino.cc/t/code-explanation-needed-please/920988/20 "2021-11-03T18:42:22Z")

</div>

> [@anon57585045](#):
>
> Subroutines have no parameters or return values.

The exact words used are programming-language dependent; don't get hung up on them (but do look for the local conventions in the language you're using.)  
Fortran has function and subroutine.  
Pascal has function and Procedure.  
C/C++ has only function (whether or not there is a return value.) (but doesn't use a keyword.)  
I've used assembly languages with half-a-dozen different ways to invoke and/or pass arguments and/or get results.

[Next page](https://forum.arduino.cc/t/code-explanation-needed-please/920988.md?page=2)
