Datatypes for functions

The Datatype section of the reference manual does not warn of some restrictions. I have testet and tried lots of combinations, but it would be nicer if this was a documented feature. (Also, I dont want to "burn" my chip with running lots of test programs on compiled and linked output :-/ )

Have I understood the below correct?

A variable can be any of the types int, byte, char, long, boolean and so on. Functions can only be of int, long and char; not byte or boolean. (What about char* ?) The same restriction seems to apply to the parameters in a function.

There is more documentation buried in the Arduino-program directory, but I couldnt find it.

Functions can only be of int, long and char; not byte or boolean. (What about char* ?)

No, functions can be of any type (including user-defined structures) and can take parameters of any type.

Get hold of a copy of Kernighan and Ritchie.

Although they can return any type, remember scoping when returning pointers.

char* setToFail() {
  char fail[] = "FAIL";
  return fail;
}

That function will not work because by the time you want to use fail, it is already 'dead' aka out of scope.

Thanks for the speed reply. The problem is elsewhere then. This works

/* This is to get the syntax of function typing right */
boolean flag ;
void setup() {
}
void loop() {
  flag = func1(23) ;
}
boolean func1( byte zz ) {
  return zz==5 ;
}

If I try and split this into modules it fails, although I do not understand why the byte variable type should be undefined.

/* test to check syntax of functoin prototyping */

/* NB: syntax errors in .h moduloe are shown as something else?!?! in the calling file */
#include "inc.h"
boolean flag ;
void setup() {
}
void loop() {
  flag = func1(23) ;
}

The inc.h

/* simple include */
const int t3 = 23 ;
const byte t4 = 2 ;     // ! This yields errors
/* must declare prototype in h for modules called accross */
boolean func1( byte zz ) ;  // this yields "two" errors

The sub file

/* define two functions */

boolean func1( byte zz ) {
  return zz==5 ;
}

"works" and "fails" is whether the compilation/link completes without errors Dont know if there would be other problems when I try and execute (OK, not on this simple exmaple)

If I try and split this into modules it fails

What "fails"?
The compilation "fails"(probably got some "#includes" in the wrong order, or you need prototypes)?
Or does the sketch "fail" (however "fail" is defined)?

Hi AWOL, well, I'll try and elucidate.

The first code section shows a single .PDE file, and it works (==compiles without errors)

The other code examples are the same code spilt into 3 files: A "main" .PDE file, an include file "inc.h" and a submodule file "sub1" (yes, it says "two" in the comment instead of "one", ignore that :wink: ) This fail (==compilation returns errors) claiming "byte" and "boolean" are not defined. Even a const byte x will not work in the include, yet it does in the main?

It seems the keyword "byte" works differently in the main file than in an include file. I suspect I need another "magic include" (I've seen on the forums I have to include "WProgram.h" in each submodule. No reason why, it just has to be.)

I have meandered through the great internet ... and found a piece .PDE of software that does it differently - with pros and cons

In your sketch folder (which has the same name as your "main" .PDE) you can place lots of other PDEs. They will all be included, for free. The scoping rules do not match what I read.

  • Global variables are module local whether static or not. By including them in a .h file with a "#ifndef"-construction so they only are defined once, they work fine as global variables.
  • Global functions are global, where static or not.
  • On the other hand boolean and byte, etc, work fine for everything.

It seems the keyword "byte" works differently in the main file than in an include file. I suspect I need another "magic include" (I've seen on the forums I have to include "WProgram.h" in each submodule. No reason why, it just has to be.)

int, char, and long are types that are built into the C language, and you can use them anywhere. byte and boolean are user-defined types, defined in wprogram.h; in order to use them you have to include their definitions as well (#include "WProgram.h") (This happens automagically for .PDE files as part of the preprocessing that simplifies C for Arduino users, but by the time you get to separate .h files, you have to do it yourself. See Redirecting to have some of the magic explained.)

Thank you westfw.
(thread closed)