Pages: [1]   Go Down
Author Topic: Why doesn't my function work ?  (Read 415 times)
0 Members and 1 Guest are viewing this topic.
Tychy, Poland
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi! I am a newbie programmer, and i have a simple question. I would like to make my project and simplify it by splitting it in external functions.

But i have problem, because this functions does not change the value of variables or write something in serial monitor. Why? There is a simple example:


Code:
 int variable = 15;
  
  void function()
    {
    Serial.println("I like green tea.");
    variable = 420;
    delay(10000);
    }
    

  void setup ()
  {
   Serial.begin(9600);
  }

  void loop()
  {
   void function();
   Serial.println(variable);
   delay (1000);
  }


It seems that this "function" don't work. Why? I read some articles, but i couldn't find solution myself. Thanks for help!
« Last Edit: April 07, 2013, 05:53:17 am by Skamajster » Logged

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

For a function to work, you have to call it, not provide a prototype.

"delay()" is also a void function.
Can you see how it is called is different to how you've tried to call your function?
« Last Edit: April 07, 2013, 05:51:30 am by AWOL » 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.

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why doesn't my function work ?
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3077
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you wish to invoke the function,  you need code like...
Code:

void loop()
{
    function() ;   // this calls the function
    // ... other stuff
}
Logged

Tychy, Poland
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much! This was so easy, and i tried to repair it all the yesterday evening. Stupid me smiley.
Logged

Pages: [1]   Go Up
Jump to: