Function prototypes

So…I’m assisting a friend with a program that has been compiling just fine. We added some code that resulted in a number of compile errors. However, when we comment out the code that was added, we continue to get seemingly unrelated errors that I suspect are related to not declaring function prototypes. The file is a “.ino” file. Do I need to explicitly declare functions via prototypes at the top of my program?

I’ve had errors like this in the past that seem to come and go the apparent irregularity has left me wondering what’s going on.

As always, thanks for your feedback.

Posting the code and errors would help.

From time to time if the compiler gets confused (sort of rare), or all the time if you are using:

#pragma SPARK_NO_PREPROCESSOR

I haven’t used the PRAGMA statement and have been unclear as to whether or not it a good practice. I’m assuming if I do that, then I’d have to explicitly include function prototypes before the functions are called.

I can’t post the code, as it’s very confidential. Sorry and I know it makes offering feedback far more challenging.

What about just the errors?

Here are the errors:

In file included from SparkJson/./ArduinoJson.h:7:0,
                 from SparkJson/SparkJson.h:13,
                 from aha_gps.cpp:2:
SparkJson/././DynamicJsonBuffer.h: In destructor 'ArduinoJson::DynamicJsonBuffer::~DynamicJsonBuffer()':
SparkJson/././DynamicJsonBuffer.h:20:33: warning: deleting object of polymorphic class type 'ArduinoJson::DynamicJsonBuffer' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
   ~DynamicJsonBuffer() { delete _next; }
                                 ^
aha_gps.cpp: In function 'void loop()':
aha_gps.cpp:297:33: error: 'UploadData' was not declared in this scope
aha_gps.cpp: In function 'void UploadData(int)':
aha_gps.cpp:321:21: error: 'ExtractData' was not declared in this scope
aha_gps.cpp:325:118: warning: format '%u' expects argument of type 'unsigned int', but argument 5 has type 'uint32_t {aka long unsigned int}' [-Wformat=]
aha_gps.cpp:325:118: warning: format '%u' expects argument of type 'unsigned int', but argument 5 has type 'uint32_t {aka long unsigned int}' [-Wformat=]
aha_gps.cpp: In function 'void PutDrec(int)':
aha_gps.cpp:333:6: error: redefinition of 'void PutDrec(int)'
aha_gps.cpp:12:6: error: 'void PutDrec(int)' previously defined here
aha_gps.cpp: In function 'String GetGpsData()':
aha_gps.cpp:357:8: error: redefinition of 'String GetGpsData()'
aha_gps.cpp:8:8: error: 'String GetGpsData()' previously defined here
aha_gps.cpp:434:42: error: 'checksum' was not declared in this scope
aha_gps.cpp: In function 'boolean checksum(String)':
aha_gps.cpp:550:46: error: 'parseHex' was not declared in this scope
aha_gps.cpp: In function 'void requestConfiguration()':
aha_gps.cpp:581:6: error: redefinition of 'void requestConfiguration()'
aha_gps.cpp:18:6: error: 'void requestConfiguration()' previously defined here
aha_gps.cpp: In function 'void getGSconfigHandler(const char*, const char*)':
aha_gps.cpp:594:6: error: redefinition of 'void getGSconfigHandler(const char*, const char*)'
aha_gps.cpp:15:6: error: 'void getGSconfigHandler(const char*, const char*)' previously defined here
aha_gps.cpp: In function 'bool CheckKeyboard()':
aha_gps.cpp:356:1: warning: control reaches end of non-void function [-Wreturn-type]
aha_gps.cpp: In function 'String GetGpsData()':
aha_gps.cpp:447:1: warning: control reaches end of non-void function [-Wreturn-type]
make[1]: *** [../build/target/user/platform-10aha_gps.o] Error 1
make: *** [user] Error 2
Error: Could not compile. Please review your code.

Here’s the top of the program where we’re trying to declare prototypes, assuming they might be needed:

// This #include statement was automatically added by the Particle IDE.
#include "SparkJson/SparkJson.h"
#include "Serial5/Serial5.h"
#include "string.h"
#include "math.h"


String GetGpsData(){
    return "fred";
}

void PutDrec(int i){   
}

void getGSconfigHandler(const char *name, const char *data){
}

void requestConfiguration() {
}

Yeah, sort of like C++. :wink:

The Particle IDE is creating your function prototypes as part of the compilation/linking processes. It makes it easier if you don’t have to do it, but not much of an issue doing it… unless you find yourself all-of-a-sudden making a change where they are required (like using the pragma directive above, skipping the Particle pre-processor). Then you find yourself hunting them all down to get them all in place!

this suggest that you are completely copying your functions… Are you using this format?

void myFunction(int something); // a function prototype

void setup()
{

}

void loop
{
  myFunction(someValue);
}

void myFunction(int something) // a function
{
  //do things here
}

An interesting tidbit…we tried to compile a copy of the application that we’re currently running and got the same kind of error. It’s almost as if the compiler is having a bad day.

Perhaps my mistake is including the {} in my prototypes?

1 Like

Prototypes should not include {}.

We’ll try that! As a rule of thumb, is it wise to have prototypes in the program (.ino)?

It’s always good practice to have prototypes - that’s actual C/C++ programming.
The prototypes are usually that stuff that goes into the header files and the implementation can follow anywhere later on.

Not having prototypes is mere convenience :wink:

2 Likes