Go Down

Topic: When are function prototypes necessary? (Read 1 time) previous topic - next topic

RBurnside

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: [Select]

#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;
}

PaulS

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 &).

davekw7x

#2
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.

Regards,

Dave

RBurnside

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

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview