2

Is it possible to manually declare a global variable that is inside of a function. The only current way I know how to do so is to declare that variable within the setup() function or simply outside of any function, but I was hoping there was a way to manually declare a variable as global so that I may call one variable from within one function in another function.

6

You need to research 'Scope'.

Your variable has to be at the same level as all functions that will use it. So at a simple level the answer has to be no.

int variableOne = 0;
void setup ()
{
  variableOne = 1;
}
void loop ()
{
  variableOne = 2;
}

The above code will work, but if you move int variableOne = 0; inside either function then it won't work, because the variable will not be in scope in the other function.

You can use parameters to functions.

void AFunction (const int& variable);

Will let you pass a single integer value to the function from the parent. You can't change that value, because of the const qualifier. If you remove the const then the & means pass the parameter by reference, which means you are passing in a reference (pointer) to the variable in the parent function and you can change it. A better way to change the parent's value is to return a value from the call.

int AFunction (const int& variable);

So you could use this like this:

int AFunction (const int& variable)
{
  return (++variable);
}
void loop (void)
{
  int value = 0;
  value = AFunction(value);
  // value now == 1.
}

You could use a Singleton class, but this is hardcore and I'm not sure you would see any real benefit on an Arduino. A class is a grouping of related function and variables that you see in libraries and a Singleton class means there will only ever be one during the lifetime of the program. In this way you can declare an instance of the class (i.e. CSingleInstanceClass onlyOne;) and then retrieve/set data from the setup and loop function, i.e.

void setup ()
{
  CSingleInstanceClass onlyOne;  // Single instance class
  onlyOne.SetVariableOne (1);
}
void loop ()
{
  CSingleInstanceClass onlyOne;  // Single instance class
  Serial.println(onlyOne.GetVariableOne());// Prints 1
}

Hope that helps.

  • Thank you so much! I have decided to declare the types above the setup function, even though I understand this takes up a bit of dynamic memory, but not much to worry about. I really appreciate the thoughful response, and will definitely come back to this post in the future! – tyler a Jul 25 '16 at 10:32
  • @JonathanA: Types do not consume memory. – Edgar Bonet Jul 25 '16 at 10:48
  • Global variables use 731 bytes (35%) of dynamic memory, leaving 1,317 bytes for local variables. Maximum is 2,048 bytes. This is what I was referring to. – tyler a Jul 25 '16 at 11:06
  • Types are the type of the variable, so int or char. It is referring to what you think, its just the word you used was a bit wrong. If memory is that much of an issue (I don't think it is yet) then you could look at using external RAM, an additional IC, but that will be slow compared to main RAM. – Code Gorilla Jul 25 '16 at 12:26
2

even though I understand this takes up a bit of dynamic memory

There is no way around that. They have to go somewhere and that place is RAM (exception: constants could be stored in flash).

Another variation (which goes beyond your question) is that a parent function could declare and build a collection of data in a local array or struct and pass a pointer to it to any sub-functions. It still takes RAM, but it will be on the parent function's stack, so only for the duration of parent function's execution(s); it will be released when the parent function exits.

  • Appreciate it JRobert! – tyler a Jul 25 '16 at 14:42
0

Please note that 'Scope' of a variable is limited inside the block ( a block starts at { and ends at } ), and that variable cant be accessed outside the block.

consider life time of a variable as well. using the keyword static may can help you some times. refer this link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.