Go Down

Topic: When are function prototypes necessary? (Read 2955 times) previous topic - next topic


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.


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

Code: [Select]

#include <TinyGPS.h>

TinyGPS gps;

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

void setup()

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");

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;


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 &).
The art of getting good answers lies in asking good questions.


Jan 16, 2011, 10:59 pm Last Edit: Jan 16, 2011, 10:59 pm by davekw7x Reason: 1
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.




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

Go Up