Function Prototype? (NEWBIE)

Noramally i put my functions at the end of my sketches. I would then put a prototype of the function before the Main () in a C program. Arduino examples I have found do not do use a prototype. Is this correct programming for the Arduino C??

thx in advance - rik

One of the more difficult aspects of C programming is assuring that the function prototype matches the actual definition.

To alleviate this problem, the Arduino IDE creates function prototypes for you. Normally, this works quite well. There are some situations, like functions with reference arguments, where it doesn't.

I would then put a prototype of the function before the Main () in a C program.

You need to get over this "before main()" misconception. There is nothing magic about main(), and not much magic about the position of various pieces of a program.

Global variables only need to be defined at global scope (not INSIDE some other function) (and before they are referenced in the code.)

Function prototypes only need to be before you reference the function. The Arduino environment will actually generate function prototypes for you, but there is nothing to stop you from including your own (as per "good C programming practice" as well (as long as they match!)

From a practical point of view, you can put your prototypes and global variables at the top of your .pde file "sketch." For example, here's a copy of the "BlinkWithoutDelay" example sketch, modified slightly to look more like a regular C program

/* Blink without Delay

 This example code is in the public domain.

 * Global variables
int ledPin =  13;      // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
long interval = 1000;           // interval at which to blink (milliseconds)

 * Function prototypes
void setup(void);
void loop(void);

 * Actual code
void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      

void loop()
   // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);