How to pass a variable to a function?

This works in the main loop

              //Extract Date and Time from stringbuffer               
        String timeBuffer = String (StringBuffer);
        String timeString = timeBuffer.substring(5,5 + dateLength);
        Serial.print("Raw TimeDate String = ");    
        Serial.println(timeString);

But if I put it in a Function

void getDateTime() {
              //Extract Date and Time from stringbuffer               
        String timeBuffer = String (StringBuffer);
        String timeString = timeBuffer.substring(5,5 + dateLength);
        Serial.print("Raw TimeDate String = ");    
        Serial.println(timeString);
       
          //   Format Time and Date String 
         //Split timeString into four strings
  String MonthText   = String ("Month ");
  String monthString = String (MonthText + timeString.substring(0,2));

  String Daytext     = String (" Day ");
  String dayString   = String (Daytext + timeString.substring(2,4));

  String Timetext     = String (" Time ");   
  String hourString  = String (Timetext + timeString.substring(4,6));
  String minText     = String (":");
  String minString   = String (minText + timeString.substring(6,8));

           // adding the strings together 
  Serial.print("              Date and Time is          "); 
  Serial.println (monthString + dayString + hourString + minString);
} // End DateTime Function

Then the StringBuffer variable is not seen by the function.

How do I pass the StringBuffer variable to the function?

Thanks

I'm guessing it is a scope issue, but that's just from my limited view of your code.

Yes, error is StringBuffer was not declared in this scope, so how do i pass the variable Stringbuffer from the main loop to the Function?

What's wrong with passing it as an argument? (I'm not keen on global scope - too much like BASIC. Actually, I'm not keen on the String class.)

Thats what I mean, something like

getDateTime(StringBuffer); // pass StringBuffer to getDateTime Function

If I do this

void getDateTime(StringBuffer) {
              //Extract Date and Time from stringbuffer               
        String timeBuffer = String (StringBuffer);
        String timeString = timeBuffer.substring(5,5 + dateLength);
        Serial.print("Raw TimeDate String = ");    
        Serial.println(timeString);
}

The I get variable or field getDateTime declared void, I thought void is used when no value is returned as is the case here?

This is why I need to know the correct way of doing this.

Thanks!

As I said in my first reply, I can't see your code.
void getDateTime(StringBuffer) { doesn't make sense, which is why the compiler doesn't like it.

http://www.cplusplus.com/doc/tutorial/functions/

doesn't make sense, which is why the compiler doesn't like it.

It doesn't make sense, because you haven't told the compiler what type StringBuffer is.

I'm with AMOL, though. Ditch the whole damned String class and learn to use the underlying NULL terminated char array.

Using char instead of string (this is in the main loop for now).

              //Extract Date and Time from stringbuffer (which is of type char)
  char timeBuffer[9];

  timeBuffer[9] = '\0';
  for (int x = 5; x < 5 + dateLength; x++) 
  {
    timeBuffer[x] = StringBuffer[x];
    Serial.print(StringBuffer[x]); //prints separate chars
  }
        Serial.println("");
        Serial.print("timeBuffer printed output is  ");    
        Serial.println(timeBuffer);

Output is

10041245
timeBuffer printed output is

Separate characters are printed, but timeBuffer is not printed even when I added a null char to the end.

  char timeBuffer[9];

  timeBuffer[9] = '\0';

Creating an array to hold 9 elements, then setting the 10th one, to any value, is not a good idea.

Works in main loop

              //Extract Date and Time from stringbuffer (which is of type char)
  char timeBuffer[15];
  timeBuffer[9] = '\0';
  for (int x = 5; x < 4 + dateLength; x++) 
  {
    timeBuffer[x - 5] = StringBuffer[x];
    Serial.print(StringBuffer[x]); //prints separate chars
  }
        Serial.println("");
        Serial.print("timeBuffer printed output is  ");    
        Serial.println(timeBuffer);

In Function

void getDateTime() {
              //Extract Date and Time from stringbuffer (which is of type char)
  char timeBuffer[15];
  timeBuffer[9] = '\0';
  for (int x = 5; x < 4 + dateLength; x++) 
  {
    timeBuffer[x - 5] = StringBuffer[x];
    Serial.print(StringBuffer[x]); //prints separate chars
  }
        Serial.println("");
        Serial.print("timeBuffer printed output is  ");    
        Serial.println(timeBuffer);    
} // endFunction

error is invalid types char[int] for array subscript, its still having a problem with seeing the stringbuffer variable in the function.

Which leads back to my original question - how do I get the Function to "see" the StringBuffer array (pointer?) variable in the main loop?

Which leads back to my original question - how do I get the Function to "see" the StringBuffer variable in the main loop?

Which leads back to my second reply.
Specify the type and the name of the variable in the function definition's argument list.

(I'm bored asking to see all of your code now, so I won't do it again)

I'm bored asking to see all of your code now, so I won't do it again

Exactly. Snippets-are-us is down the road a ways. If you want to post just snippets, go there. If you want help here, post all of your code, AND the exact error messages.

Sorry I must have missed where you asked to see all of my code.

I`ll work on some full test code, thanks for the help so far guys.

Replies 1 and 5.

You didn't really read the page I linked earlier, did you ?

So to pass an array (pointer?) name, you have to put a the "star" character in front of it.
I think that what its doing anyway...

Should it be added to this?

/*
   Demo Code for passing Array (pointer?) to function
*/
byte inByte[] = {240, 240, 240 ,240, 240, 199, 23, 1, 1, 8, 49, 48, 49, 48, 51, 51, 48, 10, 0, 49, 50, 51, 52, 53};
int counter = 0;
int bufferLength = 23;

void setup() { 
  
  Serial.begin(1200);
  Serial.println("Serial Connected");
}

void loop() {     
     //Print some Labels
      Serial.println("INDEX \t \t DEC \t \t ASCII");
      byte Buffer[bufferLength];
      char StringBuffer[bufferLength];
      for (int x= 0; x < bufferLength; x++)
      {
        Buffer[x] = inByte[x];
        Serial.print(x);
        Serial.print("\t \t");     //print a tab
        Serial.print(Buffer[x]);
        Serial.print("\t \t");     //print a tab          
        StringBuffer[x] = Buffer[x];
        Serial.println(StringBuffer[x]);
        } //EndFor 
        
     getDateTime(StringBuffer);
               
 delay(10000);  
} //End Loop  

void getDateTime(char *StringBuffer)
{
        //Extract Date and Time from stringbuffer (which is of type char)
      char timeBuffer[12];
      for (int x = 0; x < 10 ; x++)
      {
        timeBuffer[x] = StringBuffer[x + 10];
      }
      Serial.println("");
      Serial.print("timeBuffer printed output is  ");    
      Serial.println(timeBuffer);               
}

Lakes:
So to pass an array (pointer?) name, you have to put a the "star" character in front of it.

No. To declare that something is a pointer, you put a star '*' in front of the variable name in the declaration. That's what's happening in those function declarations.

http://www.cplusplus.com/doc/tutorial/pointers/
:astonished: dealing with pointers is why I`ve avoided "C" type programming until now, gives me a headache!:grin:

dealing with pointers is why I`ve avoided "C" type programming until now, gives me a headache

But, once you understand them, they are so useful and easy that you'll wonder why you ever had trouble.