Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c function that returns different values on each call

Tags:

c

atmel

I am working on the atmel programming and I am trying to make a program that has a function that returns a Boolean that is once true and the second time is false that depends on whether is it called before or not like it returns true the first time an the second it returns false and I would make the program like::

if(boolean == true){

//execute the code if the button is pressed for the first time

//switch on a led


}else{

//execute the code if the button is pressed for the second time

//turn off the led

}

the function is like::

bool ledToggle(){

if(function is called for the first time)

return true;

}else{

return false;

}
like image 336
user3407319 Avatar asked Jan 24 '26 02:01

user3407319


1 Answers

You can just use a static flag for this, e.g.

bool ledToggle()
{
    static bool state = false;

    state = !state;

    return state;
}
like image 152
Paul R Avatar answered Jan 25 '26 17:01

Paul R