0

I doubt regarding c program, how to store values in a function,if I call a function the variables are initialized and previous values are deleted so how can I store values in functions.

#include <avr/io.h>
#include <util/delay.h>
#include "buzzer.h"

void ReachDestinationAvoidingNode(unsigned char Xd,unsigned char Yd,unsigned char Xn,unsigned char Yn)
{

}


//Do not make changes in main function

int main(void)
{
       ReachDestinationAvoidingNode(5,'D',6,'D');
       buzzer_on();
       _delay_ms(500);
       buzzer_off();
       ReachDestinationAvoidingNode(2,'F',2,'D');
       buzzer_on();
       _delay_ms(500);
       buzzer_off();
       ReachDestinationAvoidingNode(2,'A',2,'C');
       buzzer_on();

}

now when the ReachDestinationAvoidingNode(5,'D',6,'D') function is called the 5,D values should be stored and again when the ReachDestinationAvoidingNode(2,'F',2,'D') called the 5,D values should be used. how to do that, without disturbing main code

Zahid Khan
  • 2,130
  • 2
  • 18
  • 31
donthi
  • 3
  • 1

1 Answers1

-2

You can add four extra class variables in your code as memXd,memYd,memXn,memYn and initialize them with null value. And in your ReachDestinationAvoidingNode() method just initialize them with new value at the end of your function. Like

    unsigned char memXd='';
    unsigned char memYd='';
    unsigned char memXn='';
    unsigned char memYn='';
    void ReachDestinationAvoidingNode(unsigned char Xd,unsigned char Yd,unsigned char Xn,unsigned char Yn)
     {
        //do your stuff;
        memXd=Xd;
        memYd=Yd;
        memXd=Xn;
        memYd=Yn;
        // by this your function will always remember previous value you can use it in your code
     }
     int main(void)
     {
       ReachDestinationAvoidingNode(5,'D',6,'D');
       buzzer_on();
       _delay_ms(500);
       buzzer_off();
       ReachDestinationAvoidingNode(2,'F',2,'D');
       buzzer_on();
       _delay_ms(500);
       buzzer_off();
       ReachDestinationAvoidingNode(2,'A',2,'C');
       buzzer_on();

     }
  • i have done what you have said but it is showing error as empty char constant what to do – donthi Nov 27 '17 at 06:15
  • you have to add a check in function whether it is emplty or not ,if empty- it means you are visiting that function for first time. – Avi Agarwal Nov 27 '17 at 06:28
  • 1
    This is seriously confusing. "Class variables" don't exist in C, and variables cannot be "empty". – unwind Nov 27 '17 at 08:59