Hi, I want to try something as a global variable. This is the expression;
I=((m/1000)-1); I know for a variable to be global it has to be declared before set up.
If I use m as a local variable in the void loop will I get the resultant value of I ?
This is a bit of a hairy one. Does anybody know?
The expression is to be included before set up.
You might be better off using a #define (macro) with a replaceable parameter.
Do you mean that you will declare a new local variable named m in setup() or that you will use the global variable m within setup() ?
why not make a function?
you hand over a paramter m and get back the return value for l.
int convert(int m)
{
return ((m / 1000) - 1);
}
You define it before "setup" (or after).
You can use it with local variables (and globals).
C++ does not work like excel where formulas get evaluated every time you refer to a cell or modify something.
The maths are done only once unless code is being executed - which only happens in functions when they are called.
So whatever is defined as part of the initialization of the variable definition is just this : an initial value.
If you want the formula to be evaluated again, it has to be injected in the code either directly (type it in or use a define) or through calling a dedicated custom function.
I would avoid Macros and just use an inline function as this gives you the benefit of type checking and avoids usual pitfalls such as parameters being evaluated multiple times if you pass an calculated expression.
PS: global variables don’t need to be declared before the setup(). You can have some elsewhere but the C++ spec says that an element can only be used after it has been declared. So if you declare a global between the setup() and the loop() you can use it in the loop() but not in the setup(). The best practice though is to declare them at the top of the file for easy identification.
I wanted "I" to be a global variable and m to be a local variable. I then hoped that whenever I varied "m" in the void loop I would get the value of I according to the formula. But from what others are saying this would not happen. The expression would need to be within the void loop.
I also cannot see Noiasca solution working. That kind of declaration with the curly brackets would never be accepted before set up. Although I stand to be corrected.
I don't think this expression with the curly brackets would be accepted before set up. It might be accepted after but then what happens to the return value. I need to assign that value to a variable "I". How would I do that?
So what should happen in my void loop? When I entered a value for m should I get a return value according to the formula?
Perhaps I will just try my solution and see if it works in a simple program. I will let the forum know the result. In the meantime somebody else might like to try it and see if it works.
Of course it would. It's simply a function definition - the same as void setup()
itself is. The difference is, that 'convert' returns an int value, while setup returns nothing.
You can assign the returned value to a ( global or local) variable with:
I = convert(m);
You should learn about functions and their definitions and usage
well, if you don't believe, you should try.
int l; // a global variable accepting the result
int convert(int m)
{
return ((m / 1000) - 1);
}
void setup() {
Serial.begin(115200);
int m = 12345; // a local variable in scope of setup
l = convert(m); // apply the formular
Serial.println(F("He who has believed and has been baptized shall be saved; but he who has disbelieved shall be condemned. (Mark 16:16)"));
Serial.println(l);
Serial.println(F("Q.E.D"));
}
void loop()
{}
for future questions:
It's always helpfull when you post your code stripped down to the problem you want to show us.
The easier we can put your problem in the IDE and press compile the faster you get answers.
The better your information is - the better answers you will get.
did we need Mark 16:16
as part of the demonstration?
I'd suggest telling the compiler it can be inlined (although compiler does what it wants)
inline int convert(int m) { return ((m / 1000) - 1);}
a) That's the freedom of the writer of a sketch. If there would have been a sketch by the TO, Mark could have been left away.
b) it tells the TO that if he doesn't believe the proposal he might be on the wrong way.
[off topic]
it is indeed your freedom - for sure.
My point (and my freedom to say so) was that I was unconvinced that bringing religion into the debate was helpful and that it was adding an irrelevant distraction to a point you were making very clearly.
It's all science, deterministic specifications and facts, wether you are baptized, saved, a sinner, a strong disbeliever or already condemned, it won't change the facts that you are right that with the function working fine.
[/off topic]
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.