Go Down

Topic: [SOLVED] Optional arguments not working - trying to avoid function overloading (Read 438 times) previous topic - next topic

montymintypie

Hi all,

I'm trying to write myself a function with optional arguments, and DO NOT want to use function overloading as it seems nonsensical in this case. I've tried to do it the normal way:

Code: [Select]
void loop() {
 writeString(String(selectedPattern));
}

// Given a string, this will write it to the display.
void writeString(String str,int delayTime = 0) {
 int StringLength = str.length();
 if(StringLength > 4 || StringLength == 0) return;
 for(int i=0;i<4-StringLength;i++) {
   Serial.write(0x78);
 }
 for(int i=0;i<StringLength;i++) {
   Serial.write(str.charAt(i));
   delay(delayTime);
 }
}


Compiling this, however, gives this error:
Quote
'writeString' was not declared in this scope


What am I doing wrong here? This is driving me nuts! I don't want to do function overloading as it's messy and takes up more space (for a single line of code too!) Removing the delay() and the default value in the call fixes it (but breaks it in another section where I define a delay).

montymintypie

Strangely, a helper function actually calls writeString("TEST") at one point and it compiles fine! More to the puzzle...


montymintypie



That fixed it... Seeing as the other function worked fine, I assumed it was something like that, just never thought to put it before loop(). Thanks!

Go Up