How do you initiate functions in the variable declaration area?

Hi all,

I was watching this video on how to use tabs:

And noticed that he initiated his functions in the variable declaration above the setup loop. How do you do that?

When I try, I get the error message:
Expected constructor destructor or type conversion
because my functions aren't in the setup or the loop.

I want to do this:

void helloWorld();

void setup() {
  Serial.begin(9600);
}

void loop() {
  helloWorld();
}

helloWorld() {
  Serial.println("HelloWorld");
}

Thanks!

(deleted)

If that's the case, then the the very first line in the code doesn't matter/can be deleted. And if that's the case, does that mean that you can't declare functions in the variable declaration section?

Thanks!

SheCurvesMobius:
If that's the case, then the the very first line in the code doesn't matter/can be deleted. And if that's the case, does that mean that you can't declare functions in the variable declaration section?

Thanks!

Sure you can... you declared a function but didn’t define it.

I'm afraid I'm not catching on to the nuances. Please remember I'm a bit of a noob. How do I define a function?

Could I write:

#define helloWorld;
void helloWorld();

void setup() {
  Serial.begin(9600);
}

void loop() {
  helloWorld();
}

helloWorld() {
  Serial.println("HelloWorld");
}

I'm pretty sure that this doesn't work... could you explain further what you mean? Perhaps write out the full example of code, not just one line?

(deleted)

Yes that's helpful.

But: if you have to write void below the loop function even though it's already written above the loop function, you're telling the compiler it exists twice.

Is there a way to declare the function above the loop (e.g.):

void helloWorld();

and invoke it below the loop without declaring it a second time (e.g.)?

helloWorld() {
  Serial.println("HelloWorld");
}

No, there is no way to do that. It would be confusing, and a risk for programming errors if the return type was only specified in the declaration.

Perhaps "void" could have been made a default return type, but it isn't. Go take it up with a standards committee. :slight_smile:

So basically, there's no benefit in declaring a function above the loop, since you have to do it later on anyways. Is this correct?

-Thanks!!

(deleted)

The OP is very confused about terminology. "initiating" a function is a meaningless term. A function can be declared. A function can be defined. A function can be called, or invoked. It cannot be "initiated".

A declaration does nothing more than tell the compiler that somewhere, you will be defining a function with the given name. That function will accept the given arguments, and return the given return type. For example:

void printMe(char *s);

This is a declaration that tells the compiler somewhere further down in the program you will define a function named "printMe", and that function will take a pointer to a character string as an argument, and will return nothing. It says NOTHING about WHAT that function does, or HOW it does it.

void printMe(char *s)
{
Serial.println(s);
}

This is a definition of the function printMe. The function name, number and type of arguments, and return type MUST hatch the declaration. The definition DEFINES WHAT the function does, and HOW it does it.

Elsewhere in the program, you can call, or invoke, the function:

void loop()
{
printMe("hello, world!");
}

This is where the function actually gets executed.

Once the function has been declared, it can be referenced in the following code, even if it has not yet been defined, since the compiler knows from the declaration the name, number and type of arguments, and return type from the declaration. You cannot reference a function that has not yet been declared.

If you declare the function, but never define the function, then the compile will FAIL when it reaches the link phase, which is where it actually creates the executable code. The link will FAIL because you've told the compiler you would be providing the definition of a function called printMe, but you never did. So, it has NO code for the function.

If you DEFINE a function before CALLING the function, then the definition IS the deslaration, and a separate declaration is not required. So, best practice is to put all function definitions near the top of the file BEFORE defining any functions that call them, though this is not always possible, when you have many functions calling many other functions.

In Arduino, it is almost never necessary for the user to declare functions in an ino file. The build system will create the necessary declarations as one of the first steps of the compilation process. This is NOT standard c/c++ behavior, but it unique to the Arduino build process to make life easier for "newbies". It also creates a LOT of problems....

Regards,
Ray L.

RayLivingston:
A function can be called, or invoked. It cannot be "initiated".

A really evil function can even be "summoned". :slight_smile:

Thanks all for sharing your insights on this subject with me--much appreciated!!