Function prototype with newline compiler error?

Please forgive me if this is the incorrect place to post a newbie question and if you can, please direct me to the appropriate place if it is not.

I am new to Arduino but have been programming in C++ for many years. Perhaps too many years because I am getting a compiler error in Arduino that my Windows compiler doesn't have an issue with.
This compiles fine in Arduino

static bool afunc(void);
static bool afunc(void)
{
  return false;
}

But when I add a newline before afunc I get a compile error.

static bool 
afunc(void);

static bool 
afunc(void)
{
  return false;
}

I believe that C++ is a free form language where spaces, tabs and newlines do not effect compilation. Does anyone know why I am getting a compile error? The error is strange too.

x.ino:116:8: error: two or more data types in declaration of 'logTemperature' *<-- note that logTemperature is a function much lower down in the code.*

  116 | static bool

      |        ^~~~

x:117:1: warning: ISO C++ forbids declaration of 'afunc' with no type [-fpermissive]

  117 | afunc(void)

      | ^~~~~

x:117:1: error: ambiguating new declaration of 'int afunc()'
x.ino:114:1: note: old declaration 'bool afunc()'

  114 | afunc(void);

      | ^~~~~

x.ino:114:1: warning: 'bool afunc()' declared 'static' but never defined [-Wunused-function]
x:114:1: warning: 'bool afunc()' used but never defined

Any help would be greatly appreciated.

Welcome to the forum

Please post the full error message

Which board are you compiling for ?

Thank you so much for your reply! It is for the Teensy 4.1 board. I put the error message in the original message but here as well. Also, log Temperature is a function much further down in the code.

x.ino:116:8: error: two or more data types in declaration of 'logTemperature'

  116 | static bool

      |        ^~~~

x:117:1: warning: ISO C++ forbids declaration of 'afunc' with no type [-fpermissive]

  117 | afunc(void)

      | ^~~~~

x:117:1: error: ambiguating new declaration of 'int afunc()'

x.ino:114:1: note: old declaration 'bool afunc()'

  114 | afunc(void);

      | ^~~~~

x.ino:114:1: warning: 'bool afunc()' declared 'static' but never defined [-Wunused-function]

x:114:1: warning: 'bool afunc()' used but never defined

Also post ALL the code in code tags.

I put your code intyo my dummy test module and got oner clean compile and one not. The clean compile happens when I put this code as the last lines

static bool 
afunc(void);

static bool 
afunc(void)
{
  return false;
}

but if I place those lines before the setup I get all kinds of weird errors.
It does appear that the Arduino Language differs from C++ in this regard.

Ah ok! Thanks! Yes my snippet is before setup. Thanks very much!

Sorry but again very new here. What part of the code I posted wasn’t in code tags? Just trying to learn. Thanks

I wonder if the 'split' version of the function definition is not recognised by the "special automatic function prototype generator feature" causing yet another prototype to be thrown in.
I can't test it now but the preprocessor compiler output should show if this is the case.

The automatic prototype generation can get quite confused when things aren't on the same line.

Sketch:

static bool
afunc(void);

static bool
afunc(void)
{
  return false;
}

void setup(){}
void loop(){}

After prototype generation:

#include <Arduino.h>
#line 1 "/tmp/rjl/0611/forum_1388103/forum_1388103.ino"
static bool
afunc(void);

static bool
#line 10 "/tmp/rjl/0611/forum_1388103/forum_1388103.ino"
void setup();
#line 11 "/tmp/rjl/0611/forum_1388103/forum_1388103.ino"
void loop();
#line 5 "/tmp/rjl/0611/forum_1388103/forum_1388103.ino"
afunc(void)
{
  return false;
}

void setup(){}
void loop(){}
1 Like

Must be because Arduino Cpp doesn't support double-line var initialization.
Correct code:
static bool afunc(void);
Hope this article was helpful

Indeed this is most likely but where did an int version of afunc() come from ?:

I think it was, my comment is just standard text.

void loop();

afunc(void)
{
  return false;
}

This is one of the things that the :enraged_face: :angry: -fpermissive :enraged_face: :angry: option (which is on in the AVR core) enables: implicit function return type int.

2 Likes

Hi @mikebeachbum. I have submitted a formal bug report to the Arduino developers on your behalf:

Until such time as the bug might be fixed, the workaround will be to refrain from adding a line break after the type in your function definitions.

Thank you very much for your and everyone’s help with this. This is a great development community that I am very happy to being a part of.

2 Likes

Generating unwanted function return types is almost as bad as generating unwanted function prototypes :grinning_face:

Incidentally, this behaviour of automatic function generation was already present in the "Processing" development system on which the Arduino "language" was based. That is, according to deepseek AI.

So the Arduino IDE developers are not necessary to blame for the consequences of all this and need not be too defensive about it IMHO.

The problems caused by the inclusion of the -fpermissive flag in the compilation command templates, and the proposal for removal of the flag are tracked by the Arduino developers here:

The programming language used by Processing is essentially Java (I think they actually do the same as Arduino and consider it to be a new language). I don't know anything about Java, but my understanding is that, unlike C++, function prototypes (AKA "forward declarations") are not used in Java.

It might well be that prototype generation was already in place in the Wiring IDE at the time Arduino forked it though.

1 Like

I'd forgotten about the "Wiring" phase in the evolution of Arduino. That was all before my time :slight_smile: . The lesson anyway is that well meaning efforts, such as those referenced here, to make life simpler for new users have to be very carefully thought through to avoid creating bigger problems in the future.

Interesting is that the small sketch produced by the OP managed simultaneously to demonstrate two of these controversial issues.