Go Down

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


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() {

// 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++) {
 for(int i=0;i<StringLength;i++) {

Compiling this, however, gives this error:
'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).


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



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