Pages: [1]   Go Down
Author Topic: Help with understanding function.  (Read 482 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 181
So, you want to make something, huh?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found this code in the second project of the book "30 arduino projects for the evil genius".
Code:
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. smiley
Logged

If you want to meet a beautiful nurse you must be patient.

Belgium
Offline Offline
Full Member
***
Karma: 0
Posts: 187
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"Evil Genius" indeed.
I mean, imagine teaching the younglings the Dark Ways of "delay".
Logged

"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.

Pages: [1]   Go Up
Jump to: