# Returning values from Sub functions

**URL:** <https://forum.arduino.cc/t/returning-values-from-sub-functions/970417>\
**Category:** Programming\
**Created:** [March 17, 2022, 4:42pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417 "2022-03-17T16:42:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayk05](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rayk05](https://forum.arduino.cc/u/rayk05)\
**Post date:** [March 17, 2022, 4:42pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/1 "2022-03-17T16:42:23Z")

</div>

Can someone tell me why this code will not work, and how I can fix it? The goal of the sketch is to turn 2 LEDs on and off based on the input to the serial monitor. It works if I include the code in function captureChar() in the void loop. But if I separate and simply call it instead, it does not work. I'm not confident with returning values from a function, and I guess that's where my error is. My serial inputs are strings that look like this: "00" or "11" or "01" or "10".

```cpp

String readString, mot1, mot2;

void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
}

int captureChar(int n1,int n2) {

  while (Serial.available()) {
    delay(3); //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read(); //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      
      // expect a string like 01 containing the LED status     
      mot1 = readString.substring(0,1); //get the first character
      mot2 = readString.substring(1,2); //get the second character 
      n1 = mot1.toInt();
      n2 = mot2.toInt();
    readString="";
      return(n1,n2);
    
  } 
}

void loop(){
  int led1, led2;
  captureChar(led1,led2);
  digitalWrite(13,led1);
  digitalWrite(12,led2);
}

```

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [March 17, 2022, 4:44pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/2 "2022-03-17T16:44:31Z")

</div>

> [@rayk05](#):
>
> `return(n1,n2)`

You can only return a single value.

If you need to return more, you can return them in a struct, or via pointers or references.

> [@rayk05](#):
>
> `captureChar(led1,led2);`

...and you need to assign the returned value to a variable

---

<div class="post-metadata">

**Author:** ![rayk05](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rayk05](https://forum.arduino.cc/u/rayk05)\
**Post date:** [March 17, 2022, 4:49pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/3 "2022-03-17T16:49:00Z")

</div>

> [@anon73444976](#):
>
> If you need to return more, you can return them in a struct, or via pointers or references.

I have to admit I haven't worked with structs/pointers/references. What would the code to do this look like?

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [March 17, 2022, 4:52pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/4 "2022-03-17T16:52:34Z")

</div>

Via reference

```cpp
void captureChar(int& n1,int& n2) {

```

Via pointer

```cpp
void captureChar(int* n1, int* n2) {

```

I suggest using reference, then all you need to do is remove the "return"

---

<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:** [March 17, 2022, 6:08pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/5 "2022-03-17T18:08:41Z")

</div>

@rayk05  
**1.** Since **return** statement delivers only one value to the calling program, you can declare led1 and led2 as global variables and then assign their values in the called function. Also, you can bring some simplication in your codes with respect to receiving characters from the InputBox of the Serial Monitor.

Your sketch with slight revision (tested on UNO):

```cpp
String readString, mot1, mot2;
int led1, led2;

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
}

void captureChar() //(int n1, int n2) Edit
{

  /* while (Serial.available())
    {
     delay(3); //delay to allow buffer to fill
     if (Serial.available() > 0)
     {
       char c = Serial.read(); //gets one byte from serial buffer
       readString += c; //makes the string readString
     }
    }*/

  byte n = Serial.available();
  if (n == 2)
  {
    readString = (char)Serial.read();//1 = 0x31
    readString += (char)Serial.read();//1 = 0x31

    // if (readString.length() > 0)
    //{
    Serial.println(readString); //see what was received 11

    // expect a string like 01 containing the LED status
    mot1 = readString.substring(0, 1); //get the first character
    mot2 = readString.substring(1, 2 ); //get the second character
    led1 = mot1.toInt(); //1
    led2 = mot2.toInt(); //1
    readString = "";
    // return (n1, n2);
  }
}

void loop()
{
  //int led1, led2;
  captureChar(); //(led1, led2); Edit
  digitalWrite(13, led1);
  digitalWrite(12, led2);
}

```

**2.** You can use structure type data as shown below to return values for led1 and led2 (tested on UNO).

```cpp
String readString, mot1, mot2;
//int led1, led2;

struct myTag
{
  int led1, led2;
};

struct myTag led;

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
}

myTag captureChar()
{

  /* while (Serial.available())
    {
     delay(3); //delay to allow buffer to fill
     if (Serial.available() > 0)
     {
       char c = Serial.read(); //gets one byte from serial buffer
       readString += c; //makes the string readString
     }
    }*/

  byte n = Serial.available();
  if (n == 2)
  {
    readString = (char)Serial.read();//1 = 0x31
    readString += (char)Serial.read();//1 = 0x31

    // if (readString.length() > 0)
    //{
    Serial.println(readString); //see what was received 11

    // expect a string like 01 containing the LED status
    mot1 = readString.substring(0, 1); //get the first character
    mot2 = readString.substring(1, 2 ); //get the second character
    led.led1 = mot1.toInt(); //1
    led.led2 = mot2.toInt(); //1
    readString = "";
    return (led);
  }
}

void loop()
{
  //int led1, led2;
  led = captureChar();
  digitalWrite(13, led.led1);
  digitalWrite(12, led.led2);
}

```

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [March 17, 2022, 6:24pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/6 "2022-03-17T18:24:36Z")

</div>

> [@GolamMostafa](#):
>
> `void captureChar(int n1, int n2)`

{Facepalm}

---

<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:** [March 17, 2022, 6:29pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/7 "2022-03-17T18:29:03Z")

</div>

> [@anon73444976](#):
>
> {Facepalm}

The called function is not returning any value to the calling program; so, the return type should be void. This is my understanding.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [March 17, 2022, 6:33pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/8 "2022-03-17T18:33:12Z")

</div>

And what about n1 and n2?

---

<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:** [March 17, 2022, 6:34pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/9 "2022-03-17T18:34:04Z")

</div>

These are the formal parameters to which the actual paramteers are being copied.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [March 17, 2022, 6:34pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/10 "2022-03-17T18:34:56Z")

</div>

And used for precisely . . . what?

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [March 17, 2022, 6:37pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/11 "2022-03-17T18:37:04Z")

</div>

have a look at [function parameters pass by value and pass by reference](https://www.tutorialspoint.com/differences-between-pass-by-value-and-pass-by-reference-in-cplusplus)

---

<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:** [March 17, 2022, 6:42pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/12 "2022-03-17T18:42:24Z")

</div>

> [@anon73444976](#):
>
> And used for precisely . . . what?

You mean that the called function should have the following codes instead:

```arduino
    n1 = mot1.toInt(); //1
    n2 = mot2.toInt(); //1
    led1 = n1;
    led2 = n2;

```

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [March 17, 2022, 6:47pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/13 "2022-03-17T18:47:45Z")

</div>

> [@GolamMostafa](#):
>
> You mean that the called function should have the following codes instead:

No, certainly not

---

<div class="post-metadata">

**Author:** ![Perehama](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perehama/32/256713_2.png) [@Perehama](https://forum.arduino.cc/u/Perehama)\
**Post date:** [March 17, 2022, 6:56pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/14 "2022-03-17T18:56:39Z")

</div>

```cpp
int readString[2];

void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  pinMode(12,OUTPUT);
}

bool captureChar() {
  if (Serial.available()) {
    delay(3); //delay to allow buffer to fill
    int c = Serial.read(); //gets one byte from serial buffer
    if (c >= '0' && c <= '1') {
      int x = c - '0';
      c = Serial.read();
      if (c >= '0' && c <= '1') {
        readString[0] = x;
        readString[1] = c - '0';
        while(Serial.available()) Serial.read();//flush buffer
        return true;
      }
    }
    while(Serial.available()) Serial.read();//flush buffer
  }
  return false;
}

void loop(){
  if(captureChar()) {
    digitalWrite(13,readString[0]);
    digitalWrite(12,readString[1]);
  }
}

```

---

<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:** [March 17, 2022, 7:03pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/15 "2022-03-17T19:03:08Z")

</div>

> [@anon73444976](#):
>
> No, certainly not

Then, let me do the followings which are simple as led1 and led2 are global:

```arduino
void captureChar()
{

```

And also:

```arduino
captureChar();

```

---

<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:** [March 17, 2022, 7:11pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/16 "2022-03-17T19:11:01Z")

</div>

> [@Perehama](#):
>
> `while(Serial.available()) Serial.read();//flush buffer`

Why are you flushing the serial buffer? Two characters have arrived and you have read them already. Now, the buffer is empty.

---

<div class="post-metadata">

**Author:** ![Perehama](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perehama/32/256713_2.png) [@Perehama](https://forum.arduino.cc/u/Perehama)\
**Post date:** [March 17, 2022, 7:16pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/17 "2022-03-17T19:16:55Z")

</div>

In case in the 3 ms more were sent than 2. for a hobby project, it is probably optional.

---

<div class="post-metadata">

**Author:** ![rayk05](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rayk05](https://forum.arduino.cc/u/rayk05)\
**Post date:** [March 17, 2022, 8:22pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/18 "2022-03-17T20:22:02Z")

</div>

Thank you for all the input everyone. @anon73444976 's suggestion to use references worked out for me. To be honest, this sketch was for me to test this method to see if I could use it as a button, and I chose to do it with an LED. I have another sketch where I am using this bit of code. This other sketch is used to smooth out the motion of a servo motor in a simple way and it uses a button to do so. However, moving forward into my project, my button is replaced by a virtual one, which I have set up to send "1" when pushed and "0" when not continuously via the serial monitor. This button is on NodeRed, a

> web browser-based flow editor, which can be used to create JavaScript functions.

The reason I wanted to use multiple arguments was that at some point I am going to be using 6 servos, which basically means I would need 6 switches. Now, this sketch worked out great using the references, for the LEDs. However, when I use it to run a single servo, my servo is basically going haywire. It goes back and forth at a single spot as if the button were being pushed and released very fast, i.e., 1010101010. And I'm not sure why this happening.

Here's the code for smoothing out my servo using a physical button:

```arduino
#include<Servo.h>
int switch1;
float switch1Smoothed;
float switch1Prev;
Servo myservo1;
float a=1200.0;

void setup() {

  Serial.begin(9600);
  pinMode(A1,INPUT_PULLUP);
  myservo1.attach(9);
  myservo1.writeMicroseconds(a);
  switch1Prev=a;
}

void loop() {
//Motor 1
  switch1 = digitalRead(A1);
  Serial.println(switch1);// read switch
  switch1 = switch1 * 2400; // multiply by 10

  // ***smoothing***

  switch1Smoothed = (switch1 * 0.05) + (switch1Prev * 0.95);
  if(switch1Smoothed>=a){
  switch1Prev = switch1Smoothed;
  }
  else{
    switch1Prev = a;
  }
  // ***end of smoothing***

// Serial.print(switch1); // print to serial terminal/plotter
// Serial.print(" , ");   
// Serial.println(switch1Smoothed);
  myservo1.writeMicroseconds(switch1Smoothed);
  delay(20);
}

```

I've tested this code with a physical button and it works fine.  
And here's the code after I merge it with the one I started this post with:

```arduino

#include <Servo.h> 
int switch1;
float switch1Smoothed;
float switch1Prev;
Servo myservo1;
float a=1200.0;
String readString, mot1, mot2;

void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  myservo1.attach(9);
  myservo1.writeMicroseconds(a);
  switch1Prev=a;
}

void captureChar(int& n1){

  while (Serial.available()) {
    delay(3); //delay to allow buffer to fill 
    if (Serial.available() >0) {
      char c = Serial.read(); //gets one byte from serial buffer
      readString += c; //makes the string readString
    } 
  }

  if (readString.length() >0) {

      
    
      mot1 = readString.substring(0,1); 
      mot2 = readString.substring(1,2); 
      n1 = mot1.toInt();
      //n2 = mot2.toInt();
    readString="";
    
  } 
}

void loop(){
  captureChar(switch1);
  switch1 = switch1 * 2400; // multiply by 2400

  // ***smoothing***

  switch1Smoothed = (switch1 * 0.05) + (switch1Prev * 0.95);
  if(switch1Smoothed>=a){
  switch1Prev = switch1Smoothed;
  }
  else{
    switch1Prev = a;
  }
  // ***end of smoothing***

// Serial.print(switch1); // print to serial terminal/plotter
// Serial.print(" , ");   
// Serial.println(switch1Smoothed);
  myservo1.writeMicroseconds(switch1Smoothed);
  delay(15);
}

```

Please let me know if you think I should make a separate post about this since I do realize that my question has now kind of changed.

Thank you.

---

<div class="post-metadata">

**Author:** ![horace](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/horace/32/1014299_2.png) [@horace](https://forum.arduino.cc/u/horace)\
**Post date:** [March 17, 2022, 8:25pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/19 "2022-03-17T20:25:05Z")

</div>

should the two statemets be

```cpp
     mot1 = readString.substring(0,1); //get the first four characters
      mot2 = readString.substring(2,3); //get the next four characters 

```

---

<div class="post-metadata">

**Author:** ![rayk05](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@rayk05](https://forum.arduino.cc/u/rayk05)\
**Post date:** [March 17, 2022, 8:56pm UTC](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417/20 "2022-03-17T20:56:28Z")

</div>

I think the substring syntax calls it to be (0,1) for the first character and (1,2) for the second, and it seemed to be working when I was testing manual inputs into the serial monitor as well. So I would think the statements are correct.

[Next page](https://forum.arduino.cc/t/returning-values-from-sub-functions/970417.md?page=2)
