Pages: [1]   Go Down
Author Topic: When are function prototypes necessary?  (Read 1547 times)
0 Members and 1 Guest are viewing this topic.
Highlands Ranch, CO
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Sparkfun Autonomous Vehicle Competitor
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When are function prototypes required when programming the arduino?

I have never had to declare function prototypes for the arduino.  However, the code below does require the following prototoype:

void gpsdump(TinyGPS &gps);

In the thread at the following link, it states that the compiler will generate function prototypes automatically.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1288542862

Here's example code for compiling.  Comment out "void gpsdump(TinyuGPS &gps)" to see my issue.

Code:
#include <TinyGPS.h>

TinyGPS gps;

void gpsdump(TinyGPS &gps);
bool feedgps();

void setup()
{
      Serial.begin(115200);
      Serial1.begin(4800);
}

void loop()
{
      bool newdata = false;
      unsigned long start = millis();

      // Every 5 seconds we print an update
      while (millis() - start < 5000)
      {
            if (feedgps())
            newdata = true;
      }

      if (newdata)
      {
            Serial.println("Acquired Data");
            Serial.println("-------------");
            gpsdump(gps);
            Serial.println("-------------");
            Serial.println();
      }
}

void gpsdump(TinyGPS &gps)
{
      long lat, lon;
      float flat, flon;
      unsigned long age, date, time, chars;
      int year;
      byte month, day, hour, minute, second, hundredths;
      unsigned short sentences, failed;

      gps.get_position(&lat, &lon, &age);
      Serial.print("Lat/Long(10^-5 deg): "); Serial.print(lat); Serial.print(", "); Serial.print(lon);
      Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");

      feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors

      gps.f_get_position(&flat, &flon, &age);
      Serial.print("Lat/Long(float): "); Serial.print(flat, 5); Serial.print(", ");Serial.print(flon, 5);
      Serial.print(" Fix age: "); Serial.print(age); Serial.println("ms.");
}

bool feedgps()
{
      while (Serial1.available())
      {
            if (gps.encode(Serial1.read()))
            return true;
      }
      return false;
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The only case I'm aware of that you need to generate a function prototype is when the function takes a reference argument (preceded by the &).
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 7
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Older versions of Arduino required that functions with reference parameters had to be defined or declared before they are used in a sketch.

With arduino-0022, I can comment out both function prototypes and your sketch compiles without errors.

Regards,

Dave
« Last Edit: January 16, 2011, 04:59:55 pm by davekw7x » Logged

Highlands Ranch, CO
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Sparkfun Autonomous Vehicle Competitor
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK, now I understand.  I have never used pointers and I am using version 21.  Problem solved.  Thanks for the help.
Logged

Pages: [1]   Go Up
Jump to: