useless return

Hello,

I am working with a RFID and most people are using this code.

if( ! mfrc522.PICC_IsNewCardPresent()){
        return;
      }
      
      // Select one of the cards
      if ( ! mfrc522.PICC_ReadCardSerial()){
        return;
      }

Why is there a useless return ?
Can someone explain that to me ?

Can you explain why you think the return in your snippet is useless?

Does any code appear after those two if statements which could be executed?

Which return do you think is “useless”? Why do you think it is “useless”?

This code says...
Check if a card is present, if it is not present then get out
Check if the card can be read, if it cannot then get out

Or to put it another way, if there’s no card present it is pointless continuing and trying to read it. If you cannot read the card it is pointless continuing and doing anything else. Etc.

Makes perfect sense to me.

pcbbc:
Which return do you think is “useless”? Why do you think it is “useless”?

This code says...
Check if a card is present, if it is not present then get out
Check if the card can be read, if it cannot then get out

Or to put it another way, if there’s no card present it is pointless continuing and trying to read it. If you cannot read the card it is pointless continuing and doing anything else. Etc.

Makes perfect sense to me.

Why ? return has no value and there's nothing else in the body.
There's nothing to execute.
I don't get the logic.

Post all the code, rather than a useless little snippet, and we will explain why "return" makes perfect sense.

Return gets you out of the function you're in; if you're in loop(), then loop() gets called straight away, and you get another attempt at what you were doing.

But what the logic is is hard to tell from your snippet

jremington:
Post all the code, rather than a useless little snippet, and we will explain why "return" makes perfect sense.

Here is my code after the if-statements

void badge(){
    while(badgeOpnieuw){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Luister en scan");
      lcd.setCursor(0,1);
      lcd.print("de juiste badge");
      delay(2000);
      morse();
      while ( ! mfrc522.PICC_IsNewCardPresent()){
        return;
      }
      
      // Select one of the cards
      if ( ! mfrc522.PICC_ReadCardSerial()){
        return;
      }
      String content= "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++){
         Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
         Serial.print(mfrc522.uid.uidByte[i], HEX);
         content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
         content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      content.toUpperCase();
      
      if (content.substring(1) == "2B 41 71 0D"){
        lcd.clear();
        digitalWrite(5, HIGH);
        delay(1000);
        digitalWrite(5, LOW);
        lcd.setCursor(0,0);
        lcd.print("proficiat, pass:");
        lcd.setCursor(0,1);
        lcd.print("1234");
        delay(3000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("download arduin.");
        lcd.setCursor(0,1);
        lcd.print("bluetooth contr.");
        badgeOpnieuw = false;
        delay(3000);
      }
     
      else{
        aantalFouteBadge++;
        if(aantalFouteBadge != 1){
        Serial.println();
        Serial.println("minuten before doubling: ");
        Serial.println(minuten);
        minuten = minuten*2;
        Serial.println("minuten after doubling: ");
        Serial.println(minuten);
       }
        lcd.clear();
        digitalWrite(2, HIGH);
        delay(1000);
        digitalWrite(2, LOW);
        lcd.print("foute badge");
        tone(6, 500);
        delay(1000);
        noTone(6);
        lcd.setCursor(0,1);
        seconden = 60;
        
    
       while(true){
          lcd.setCursor(0,1);
          if(seconden < 10){
            lcd.print((String)(minuten-1)+ ":0" + (String)seconden); 
          }else{
            lcd.print((String)(minuten-1)+ ":" + (String)seconden); 
          }
          seconden--;
          if(minuten == 1 && seconden == 0){
            break;
          }
          if(seconden == 0){
            seconden = 60;
            minuten--;
          }
          delay(1000);
       }
      } 
  }

So, you return from the function badge().
What happens then we don't know, BECAUSE YOU DIDNT POST ALL OF YOUR CODE.

Fine as long as whatever calls “badge” doesn’t have anything else to do in the background. (E.g. flash a led, check other hardware).
However, if it does have other stuff to do, the way you have function “badge” coded it is now blocking.

p.s. the easiest way to count minutes and seconds it to hold your time in a single unit (e.g. seconds = 120 for 2 minutes). Don’t try to count in multiple units.
When you want to display the time remaining, divide the overall time by 60 to get minutes and seconds:

seconds = remaining % 60;
minutes = remaining / 60;
...
Delay(1000);
remaining--;

Also delay is not at all accurate. But that’s another problem...

TheMemberFormerlyKnownAsAWOL:
Return gets you out of the function you're in; if you're in loop(), then loop() gets called straight away, and you get another attempt at what you were doing.

But what the logic is is hard to tell from your snippet

Ow now I begin to understand. But is return used for getting out a function (like a break in a loop) or is return used for restart a function (like a continue in a loop) ? Is that what you mean ?

Here is my whole code

#include <SPI.h>
#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int pinRST = 9;
const int pinSS = 10;
MFRC522 mfrc522(pinSS, pinRST);

int incomingByte;
int rood = 2;
int wit = 3;
int geel = 4;
int groen = 5;
int lijstGetallen[14];
int teller = 4;
int tellerr = 1;
int seconden = 0;
int minuten = 2;
int aantalFouteBadge = 0;
boolean badgeOpnieuw = true;
boolean doorOfNiet = true;


void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  randomSeed((analogRead(0)));
  pinMode(6, OUTPUT);
  pinMode(rood, OUTPUT);
  pinMode(wit, OUTPUT);
  pinMode(geel, OUTPUT);
  pinMode(groen, OUTPUT);
  for(int a = 0; a < 14; a++){
    lijstGetallen[a] = random(2,6);
  }
  lcd.begin();
  badge();
}



void loop() {
  //hier wordt de memory game afgespeeld
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("level " + (String)tellerr);
  for(int b = 0; b < teller; b++){
     digitalWrite(lijstGetallen[b], HIGH);
     delay(500);
     digitalWrite(lijstGetallen[b], LOW);
     delay(500);
  }
 
  //hier voert de gebruiker zijn gegevens via bluetooth
  for(int c = 0; c < teller; c++){
    while(Serial.available() <= 0){
   
     }
     if (Serial.available() > 0) {
      incomingByte = Serial.read();
      switch(incomingByte){
        case '5':
          digitalWrite(groen, HIGH);
          delay(500);
          digitalWrite(groen, LOW);
          break;
        case '4':
          digitalWrite(geel, HIGH);
          delay(500);
          digitalWrite(geel, LOW);
          break;
        case '3':
          digitalWrite(wit, HIGH);
          delay(500);
          digitalWrite(wit, LOW);
          break;
        case '2':
          digitalWrite(rood, HIGH);
          delay(500);
          digitalWrite(rood, LOW);
          break;
      }
      delay(200);
      if((incomingByte-48) != lijstGetallen[c]){
        doorOfNiet = false;
        lcd.clear();
        lcd.print("failed");
        tone(6,500);
        for(int d = 0; d < 4; d++){
          digitalWrite(rood, HIGH);
          digitalWrite(wit, HIGH);
          digitalWrite(geel, HIGH);
          digitalWrite(groen, HIGH);
          delay(250);
          digitalWrite(rood, LOW);
          digitalWrite(wit, LOW);
          digitalWrite(geel, LOW);
          digitalWrite(groen, LOW);
          delay(250);
        }
        noTone(6);
      }
   }
  }

  
  //blinkende lampen en buzzer indien alles juist
  delay(200);
  if(doorOfNiet){
    tellerr++;
    if(teller < 14){
       teller++;
    }
    tone(6,1000);
    digitalWrite(rood, HIGH);
    digitalWrite(wit, HIGH);
    digitalWrite(geel, HIGH);
    digitalWrite(groen, HIGH);
    delay(500);
    digitalWrite(rood, LOW);
    digitalWrite(wit, LOW);
    digitalWrite(geel, LOW);
    digitalWrite(groen, LOW);
    noTone(6);
    delay(1000);
  }
}

void morse(){
  tone(6, 500);
    delay(250);
    noTone(6);
    delay(250);
    tone(6, 500);
    delay(250);
    noTone(6);
    delay(250);
    tone(6, 500);
    delay(1000);
    noTone(6);
    delay(250);
    tone(6, 500);
    delay(1000);
    noTone(6);
    delay(500);
    tone(6, 500);
    delay(1000);
    noTone(6);
    delay(500); 
}

void badge(){
    while(badgeOpnieuw){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Luister en scan");
      lcd.setCursor(0,1);
      lcd.print("de juiste badge");
      delay(2000);
      morse();
      while ( ! mfrc522.PICC_IsNewCardPresent()){
        
      }
      
      // Select one of the cards
      if ( ! mfrc522.PICC_ReadCardSerial()){
        return;
      }
      String content= "";
      byte letter;
      for (byte i = 0; i < mfrc522.uid.size; i++){
         Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
         Serial.print(mfrc522.uid.uidByte[i], HEX);
         content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
         content.concat(String(mfrc522.uid.uidByte[i], HEX));
      }
      content.toUpperCase();
      
      if (content.substring(1) == "2B 41 71 0D"){
        lcd.clear();
        digitalWrite(5, HIGH);
        delay(1000);
        digitalWrite(5, LOW);
        lcd.setCursor(0,0);
        lcd.print("proficiat, pass:");
        lcd.setCursor(0,1);
        lcd.print("1234");
        delay(3000);
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("download arduin.");
        lcd.setCursor(0,1);
        lcd.print("bluetooth contr.");
        badgeOpnieuw = false;
        delay(3000);
      }
     
      else{
        aantalFouteBadge++;
        if(aantalFouteBadge != 1){
        Serial.println();
        Serial.println("minuten before doubling: ");
        Serial.println(minuten);
        minuten = minuten*2;
        Serial.println("minuten after doubling: ");
        Serial.println(minuten);
       }
        lcd.clear();
        digitalWrite(2, HIGH);
        delay(1000);
        digitalWrite(2, LOW);
        lcd.print("foute badge");
        tone(6, 500);
        delay(1000);
        noTone(6);
        lcd.setCursor(0,1);
        seconden = 60;
        
    
       while(true){
          lcd.setCursor(0,1);
          if(seconden < 10){
            lcd.print((String)(minuten-1)+ ":0" + (String)seconden); 
          }else{
            lcd.print((String)(minuten-1)+ ":" + (String)seconden); 
          }
          seconden--;
          if(minuten == 1 && seconden == 0){
            break;
          }
          if(seconden == 0){
            seconden = 60;
            minuten--;
          }
          delay(1000);
       }
      } 
  }
}

It’s get out. What happens next depends on what code comes next in whatever called your badge function.

The return here is no different than the return you are used to from a function with return type int, however this is a return from a function of type void. Returns to the same place, does the same job. Just doesn’t have a value to return because the function is of type void.

pcbbc:
It’s get out. What happens next depends on what code comes next in whatever called your badge function.

The return here is no different than the return you are used to from a function with return type int, however this is a return from a function of type void. Returns to the same place, does the same job. Just doesn’t have a value to return because the function is of type void.

So take for example function badge() that is between void setup() and void loop()
So what you mean is that return leaves the current function without executing the remaining code and then ?
Does it execute the function badge() again or does it execute the next function like void loop() ?

return leaves the current function without executing the remaining code and then ?

Returns to the line immediately following the statement that called that function.

In the code you posted, badge() is called from the very last line of setup(). So when badge() terminates, setup() returns to the hidden main() function, which then repeatedly calls loop().

Time to brush up on really basic C/C++!

jremington:
Returns to the line immediately following the statement that called that function.

In the code you posted, badge() is called from the very last line of setup(). So when badge() terminates, setup() returns to the hidden main() function, which then repeatedly calls loop().

Time to brush up on really basic C/C++!

Ah thank you, you helped me a lot

iliwasel:
So take for example function badge() that is between void setup() and void loop()
So what you mean is that return leaves the current function without executing the remaining code and then ?
Does it execute the function badge() again or does it execute the next function like void loop() ?

return returns to where the current function was called from, always. A function declared as return-type void has an iimplicit "return" at the end, assuming the end is reachable.

Functions with a non-void return-type must have explicit return statements with a value to pass back (unless
they never return).