Go Down

Topic: Help with understanding function. (Read 537 times) previous topic - next topic

Ufoguy

I found this code in the second project of the book "30 arduino projects for the evil genius".
Code: [Select]

void loop()
{
flash(200); flash(200); flash(200);
delay(300);
flash(500); flash(500); flash(500);
flash(200); flash(200); flash(200);
}

void flash(int duration)
{digitalWrite(ledPin, HIGH);
delay(duration);
digitalWrite(ledPin, LOW);
delay(duration);
}


I understand what the function does, but I'm unable to understand "int duration" is doing in void flash();

Is the variable being declared ? Shouldn't it be done before void setup()

Thanks in advance. :)
If you want to meet a beautiful nurse you must be patient.

Steen

for these questions, you can look in some simple C/C++ guides.

That int duration is just a declaration of a variable (argument) needed for the function. An argument is a value the function uses to calculate something or to do st with it. You have to pass the value between the brackets '(' and ')' if you want your function to work.

Here, the function flash(int duration) uses the variable duration to know how long it has to light up the LEDs (for the delay).
as you can see in the loop() {} there is flash(200);. This means that we declare our 'int duration' as 200.
The function then recognizes the argument '200' as an int and uses that int to set the delay(duration), since 'int duration' then got the value 200.

AWOL

"Evil Genius" indeed.
I mean, imagine teaching the younglings the Dark Ways of "delay".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up