How does changing a variable inside a function affect that variable elsewhere?

Sorry if this seems like a stupid question. I've been away from any kind of coding for longer than some of you have been alive. So, still eazing my brain into it.

Say I have the following in a sketch....

int a = 0;
void setup(){
    
}
void loop(){
   firstFunction();
   secondFunction();
   //what does "a" equal now?
}

void firstFunction(){
   a = 1;
}

void secondFunction(){
   a = a+1;
}

What would "a" equal after the 2nd function does it's thing? I'm a long way from my arduino and jotting down code (or what a 5 year old might believe is code) on paper. Otherwise, I'd do a basic script like above and test it.

I'm thinking it should be 2.

I'm somewhat sure that, if the variable is created inside the function, it can't be used elsewhere unless you return it as a result. And even then you're only getting the result. And not the variable name.

Am I thinking correctly on this?

Thanks!

You are right in your thinking. Because a is declared outside of a function, it is global and visible to the entire sketch. If it were declared inside a function it would be local to that function. This thread discusses variable scope.

groundFungus:
You are right in your thinking. Because a is declared outside of a function, it is global and visible to the entire sketch. If it were declared inside a function it would be local to that function. This thread discusses variable scope.

Thank you Mr. Ground.........Fungus? Ummmm. ... Nope. Not going to ask. lol

Be careful here too with how you name your variables. The compiler will have no problem with you declaring a global variable named 'a' and then also declaring a local variable named 'a' within your function. They are not the same thing!

int a = 0;
void setup(){

}
void loop(){
   firstFunction();
   secondFunction();
   //what does "a" equal now?
}

void firstFunction(){
   int a = 7;
   Serial.print(a); //will print 7
}

void secondFunction(){
   a = a+1; 
   Serial.print(a); //will print 1
}

shralp:
Be careful here too with how you name your variables. The compiler will have no problem with you declaring a global variable named 'a' and then also declaring a local variable named 'a' within your function. They are not the same thing!

int a = 0;

void setup(){

}
void loop(){
  firstFunction();
  secondFunction();
  //what does "a" equal now?
}

void firstFunction(){
  int a = 7;
  Serial.print(a); //will print 7
}

void secondFunction(){
  a = a+1;
  Serial.print(a); //will print 1
}

Thanks for the tip. I try not to use simple named variables such as "a". I get myself confused. Ha! I just did it in that example to simplify it.