Return doesn't return data! help please

Hi, i'm in the middle of a control test program at work and i'm having an issue with return. :0
I can't post the code ( company policy!) but i can give you an idea of what isn't happening.

Im using a Mega 2560, with a 4 x 4 keypad.
Ive set global variables ;
String serialNo = "" and another called String DataNo =""

from a function GetSerialNo()
i call another function that deals with the keypad ( DataEntry (10,0)) this is setup so i send it number of digits and number of gap.
i then use switch case for data entry, confirmation and editing.
once the number is confirmed i use return DataNo, the code works fine apart from the return;

String DataEntry( int digits, int gap){
 DataNo="";
rest of code (working)
then key test in here 

case'G': {                           // if G (Start) then this is confirmed                    
        lcd.clear();
        delay(200);
        lcd.print("Confirmed ");
        lcd.setCursor(0,2);
        lcd.print(DataNo);                 // show the number then return
        delay(600);
        Serial.print("Returning;");
        Serial.println(DataNo);
        return DataNo ;
      }

so i go back to the function that called this one and try Serial.print( DataNo) and i dont get anything, its probably really simply fixed but im a newbie at Arduino coding.

this function is called by 2 different functions neither are getting info back.
Could someone please help?

but i can give you an idea of what isn't happening.

Can you post a small but complete example that illustrates your problem, because yes return does return data.

ive set global variables ;
String serialNo = "" and another called String DataNo =""

cheers for looking, i have made it a global. the rest of the code is working fine, i don't even get the info back if i set it to void and don't return the DataNo.
Initially the code was called by one function and it returned directly back to it, but now i have 2 functions nothing is happening.

so i go back to the function that called this one and try Serial.print( DataNo) and i dont get anything

If you do not get a value for DataNo from the function that calls the one you posted a section of then it is surely no surprise that you do not get anything returned from here is it ?

Where does DataNo derive from ? Can you share that portion of code ?

im checking that the data is getting upto the return , using the serial print, thats coming up fine, if i do that after the line where i have called the function from, im not getting any value.

include blurb
loads of variables

String DataNo ="";


void setup(){
begin.serial(9600);
other stuf for lcd and keypad}

void loop(){}
other functions...

void dataLog(){
lcd blurb here;
DataEntry (10,0);                                                                // call keypad info getter
Serial.print (DataNo);                                                       // serial.print the returned value and nothing here

get on with rest of code.....}


void DataEntry( int digits, int gap){
 DataNo="";                                                                      // clear previous data out before starting the function
rest of code (working)
then key test in here 

case'G': {                                                     // if G (Start) then this is confirmed                    
        lcd.clear();
        delay(200);
        lcd.print("Confirmed ");
        lcd.setCursor(0,2);
        lcd.print(DataNo);                       // show the number then return
        delay(600);
        Serial.print("Returning;");
        Serial.println(DataNo);              // checking that the data exist here and it does
        return;
      }

Ive tried it with string at the top of the function and with void, its supposed to be a global variable that i have set up before void setup();
the lcd displays the code and then it prints it to the serial port but once it hits the return it disappears.

  1. Avoid using Strings on Arduino.
  2. I can't see anywhere that you set DataNo to anything other than "", therefore why would you expect it to contain anything else?

the rest of the code in the DataEntry section sets up the DataNo, i only have the DataNo ="" at the beginning of the called function to clear it.
like i posted i can see the values in DataNo at the point just before the return statement, but not after its returned to the function.

You can return dataNo any way you like, but you're not catching the return value like x = myFunction();, it's rather useless. Plus it is a global variable, no need returning it. Basically, i see bad programming here.

if its a global variable then why don't i see it?

Basically, i see bad programming here.

=(
thanks, for that.
I thought the idea of the forum was to try and help people who are learning. Considering i've only been doing this for the last 3 weeks i dont think ive done to bad. Hence why i asked, if i knew what was wrong i wouldn't be posting it here.
Maybe its just me but theres a load of criticism and sarcasm on this site, people come on to learn not be abused!

I think ill just pootle on on my own, ill sort it out with my bad programming that actually seems to work apart from this bit! which if i dont fix tomorrow ill just remove!

        Serial.println(DataNo);              // checking that the data exist here and it does
        return;

Will this return DataNo I wonder ?
Without seeing a substantial proportion of your code, if not all of it, it is impossible to say what is causing the problem. For instance, you could have both a global and local global named DataNo, but who knows ?

Basically the code ive shown is the only locations where DataNo exist.
ive remd out the other function for now, to try and limit the possibilities.

Basically the code ive shown is the only locations where DataNo exist.

So where is DataNo set to anything except "" ?

vikingsraven:

Basically, i see bad programming here.

=(
thanks, for that.

I'm sorry you read it like that. That was never my intention. The fact is:

  • Either you have a function return a value of a local variable and use it with thefunction call.
  • or change the value of a global variable in the function, and just exit the function.

Mixing these up is considered bad programming. Below are two examples:

int myFunction()
{
    int x;

     x = 4;
     return x;
}

//usage
void loop()
{
Int myVar;
myVar = myFunction();
}
Int globalVar;

void myFunction2()  //void means i do not return a value
{
   globalVar = 17;
   return;   //not needed if it is the last statement in a function
}

Also, i apologize for typos. Editting code on an iPad in a textbox isn't my thing.

Thanks guys
UKHeliBob , apart from the DataNo that is used in the main DataEntry function , all im doing is DataNo += number , which is the number that im inputing , this is working ok.
a local global variable not heard of that, probably why ive steered away from C etc in the past! Assembler is sooo much easier to thinkout!

RobvdVeer
sorry , getting a bit stressed out over this, out off my comfort zone. normally i can step back look at it and fix it , this though has got me!
ive put the return in because its in the middle of the Switch case, if i break; it just sits there and waits.
Didnt get the myVar = myFunction();
so if i was to use GotData = DataEntry(); for example that should pull the data through?
would'nt i have to put the (int x, inty) that i use when i call the function?

The big difference here is a function returning data, or a function that doesn't return anything (a sub in Basic). It is your choice as a programmer to decide if you want to modify a global or not.

Of course you still need to provide the function call arguments, or your compiler will complain.

You should put the 'return;’ at the end of the switch case (it can replace break;) if you want the function to end there. Otherwise, execution continues after the switch statement's closing bracket.

a local global variable not heard of that, probably why ive steered away from C etc in the past! Assembler is sooo much easier to thinkout!

That was a typo on my part. No wonder you were confused. It should have said "both a global and local variable named DataNo, but who knows ?"

AGGGGGGHHHHHHHHHHH!!!
no wonder my heads exploding!
ok . ill have a look at the code again tomorrow and ill look around, thanks guys, i do appreciate the effort. :slight_smile:

HI, just for an update , my error i had loaded the local data into the return data by mistake, 10 secs after looking at it the next morning.
Thanks for your help guys

Sometimes taking a time-out and a fresh view is the best solution. I just had a similar experience with a crimping tool (aaaarrrggghhh).