Is it possible to access a local variable in a function from another function?

Part 1(had to cut it up):
Hi, right now I'm working on a project called "Candy alarm". The code is quite long, so I will briefly explain it, show the code and then talk about the problem I have.

The project - a brief overview
So basically what I want to do is make an alarm for a cupboard where I keep all my candy. When I open the door to the cupboard, I want the alarm to go off, and when the door is closed, I do not want the alarm to go off(simple, right?).

To do this, I'm using diffrent functions, each for one part of the hardware. The hardware I'm using is:
1.Aultrasoincsensor, to calculate the distance between the sensor and the cupboard door. - Function name: "void ultraSonic()"
2.A LDR, to measure the light in the cupboard. - Function name:"void LDR()"
3.LED:s, one red for when the cupboard door is open and the alarm goeas off, and a green for the opposite. - Function name: "void konsekvenser()"(swedish for consequences)
4.A LCD, so I can print on it wheter the cupboard is safe or not. I combined this with the "Serial.print" commands, as they both print the same thing. - Function name: "void printer()"
5.Another function I made was one called "void sakerhetF" (sakerhet is security in swedish, F for function), for determinating wheter the cupboard is safe or not.

What I then do with the functions is creating another function called "void godislarm()" (again, swedish for candy alarm), and putting them all in there, the running "godislarm();" in "void loop".

In answer to the subject: No. Local variables don't exist when the function they are in is not running.

You can pass a local variable to a function CALLED by the function containing the local variable.

You can return the value os a local variable to the function that CALLED the function containing the local variable.

You can make a variable GLOBAL by declaring it outside of the functions. Then any function can read or write the variable.

johnwasser:
In answer to the subject: No. Local variables don't exist when the function they are in is not running

... unless they're qualified "static".

But they're still not accessible.

AWOL:
... unless they're qualified "static".

But they're still not accessible.

... unless the routine containing the static variable has previously passed a pointer to that static variable to the calling routine or as a global.

But that would be a rather unusual thing to do.

Thanks for your fast replies! Ran into a bit of trouble due to the use of to many characters in one post, so I couldn’t post the code.
However, here is the part of the code I have trouble with:

void printer(int distance, int ldrStatus, int sakerhetA){
  String names[] = {"Ultrasonicsensor", "LDR", "Sakerhet"};
  int values[] = {distance, ldrStatus, sakerhetA}; //Array med värden <- !PROBLEM!
  
  for(int i = 0; i < 3; i++){ //For-loop som upprepas tre gånger genom variabeln 'i'
    lcd.setCursor(0,0); //skriver på andra raden på LCD:en
      lcd.print(names[i]); //Printar ut värden från arrayen names, och beroende på 'i' värde printas olika värden i arrayen ut
      Serial.print(names[i]); 
    lcd.setCursor(0,1); //skriver på andra raden på LCD:en
      lcd.print(values[i]); //Printar ut värden från arrayen values, och beroende på 'i' värde printas olika värden i arrayen ut
      Serial.print(values[i]);
    delay(2000); //Väntar i 2 sekunder
      lcd.clear(); //Rensar displayen
      Serial.println();
    }

As I’m new to the process of passing variables to functions, I would like you to help me with suggestions of different ways I can write this function. Worth mentioning, the “int values” array contains of the values calculated in respective function I described in my first post.

/Thanks again! :slight_smile:

I would make these three variables 'global' by moving the declaration outside the functions:

int distance, ldrStatus, sakerhetA;

Then printer() would not need to be passed the three values as arguments since it could read the globals:

printer(); // Print the three global values

I would also change the names to have an initial capital letter to make them more easily distinguished from locals:

int Distance;
int LDRStatus;
int SakerhetA;

Note: As global variables, they will be initialized to zero for you unless you provide a different initialization value.

An alternative is to make them local to loop() and get the value from the three functions you described:

void loop()
{
  int distance = functionThatReturnsDistance();
  int ldrStatus = functionThatReturnsLDRStatus();
  int sakerhetA = functionThatReturnsSakerhetA();



  void printer(distance, ldrStatus, sakerhetA)

}