Pages: [1]   Go Down
Author Topic: [SOLVED] Optional arguments not working - trying to avoid function overloading  (Read 380 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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).
« Last Edit: June 08, 2011, 07:00:01 am by montymintypie » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arlington, MA, USA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 259
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try defining writeString before loop
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



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!
Logged

Pages: [1]   Go Up
Jump to: