How does one code functions?

I have looked around a bunch and I can’t find a good resource to learn how to implement “custom” functions into a sketch. I’ve found multiple places that show how to create libraries, but I would like to know more about the functions in general. I’m working on a project that needs some functions and I will probably turn these into a library in the future, but I need to learn more about functions first. I’m sorry if this is an extremely basic C programming question.

In C (or C++) there is no explicit keyword associated with defining a function:

returntype functionname(paramtype param, paramtype2 param2, ...)
   // code to implement function
   return returnvalue;

So, for example, "loop" and "setup" are actually functions (functions without return values ("void") or parameters (empty parents after the name.)

You'll find this explained along the way in....

... as it is fundamental to writing good programs, whether you go on to make libraries or not.

The site listed above is a course on Arduino programming, most valuable if you actually work through it sequentially, but if you are impatient, you may get what you need from the second lesson "A Better Program", and the later one "Functions that return values"

Thanks for the advice, both were very helpful. Thanks for the site, tkbyd; I think I know most of that but there is some stuff that I don't.

I think I only have one question left, which is where does one place the "definition" of a function? It seems every example I see just places it after the void loop() function. This seems odd to me because if the void loop() function loops indefinitely, it will never get to the definition of the custom function. Does this mean when your custom function gets called, the sketch will just search throughout itself and find out what that function actually does? Is it ok to define the function at the beginning of the sketch?


This isn't how things work; to simplify, the compiler takes the code and compiles everything - each function gets an address, and these are stored in a table so that when one is called, the address is looked up, and the function is called. It isn't like a scripting interpretor and such (and even then, most such interpretors are smart enough to figure it out).

Maybe somebody else can give you a better explanation, I know mine is really rough (its like somebody trying to explain how they talk; they've been doing it so long that it is difficult or impossible to explain, it is second nature; coding and how these things work is like that for me, now - if I actually tried to explain it, the thread might explode).

As far as where to define functions?

You really can define them anywhere, even in another file (this is the reason libraries exist). Put them above the loop() function, put them below it, put 'em above the setup() function; it doesn't matter to the C compiler.

Convention in C programming, though, is typically to put the functions above the 'void main()' function, but since on the Arduino that function is abstracted away, putting them above setup() is the next best thing if you want to follow that convention. There used to be a reason for it, before it became convention (maybe it had something to do with multiple-pass vs single-pass compilers?), and it has since "stuck".

What you typically see on the Arduino, though, is the functions placed after the loop() function; this also mimics the style seen in most modern BASIC compilers, as well (but I doubt there is a link).

Hope that helps...


Thanks, that was really helpful. No, your explanation was easy enough to understand. I think I'll be able to work with these now.

Thanks all!