I
I
IgorTitov20002016-11-08 00:37:48
Arduino
IgorTitov2000, 2016-11-08 00:37:48

Why are functions created after they are used in the code examples in the book?

I'm learning how to write sketches for Arduino from the book "Programming the Arduino. Basics of working with sketches" by Simon Monk. In the book, in code examples, functions are first called and only then written.
For example like this:

void loop()
{
  flash(20, delayPeriod);
  delay(3000);
}

void flash(int numFlashes, int d)
{
  for (int i = 0; i < numFlashes; i ++)
  {
    digitalWrite(ledPin, HIGH);
    delay(d);
    digitalWrite(ledPin, LOW);
    delay(d);
  }
}

The way it is written in the book does not work, it works, of course, if you transfer the creation of the function to its use -
void flash(int numFlashes, int d)
{
  for (int i = 0; i < numFlashes; i ++)
  {
    digitalWrite(ledPin, HIGH);
    delay(d);
    digitalWrite(ledPin, LOW);
    delay(d);
  }
}

void loop()
{
  flash(20, delayPeriod);
  delay(3000);
}

Is this the wrong code in the book, or am I missing something?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2017-02-20
@IgorTitov2000

Previous versions of the IDE allowed this (when compiling, they automatically placed the functions as they should).
More recent versions of the IDE don't take it easy anymore and you have to strictly follow C++ standards.

M
Maxim Moseychuk, 2016-11-08
@fshp

You may be missing header files.

R
romy4, 2016-11-08
@romy4

or just the functions are declared before they are used

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question