There's your key. In C, declaration follows use. Or is it the other way 'round?
The function returns a value of a given type. This is the claim made by the first line of the function.
You receive that value because you called the function, so it kinda looks like the function has taken on a value
int myVersionOfS = resultShouldBe(); // resultShouldBe() is... an integer.
I'm not so sure. If we had seen you neglecting to do anything with the value which is resultShouldBe(), we might have noticed you also naively attemting to collect that value through the (mis)use of the name, s, of a variable that was long gone by the time the function returned, and we might have seen your introduction of another 's' and flagged that as well.
Or found the same things going on in a minimum reproducible sketch, the preparation of which has often been when I solve a problem you guys never get to hear about.
Good point! That's when I should have recognised the distinction 'Variable' v 'Value'.
But I tend to disagree about posting the relevance of the rest of my code. My opening comment "The code below in setup() works correctly..." was carefully constructed. It says "this has passed verification & compilation. No failures to declare. Most importantly, no ambiguous or contradictory declarations. IOW it says I had not used 's' as a variable anywhere else.
Must say I'll hesitate somewhat about packing off code into a non-void function in future. Even if (as in this case) I expect to use it several times. The price of the added complexity seems high.
This was your fundamental mistake. When it didn't compile you should have realised that you were trying to access a variable that is local to the function in your main code. By declaring another "s" variable you got passed the error, but you now have 2 "s" variables that are not connected in any way. One in the main code, and one that only exists within the function.
If you had of declared "s" as a global, and not in the function it would have worked. But using global variables within functions, kind of defeats the purpose of having a function.
You're kind of missing the point of using functions...
A function should be passed what it needs (parameters), and return a result (although sometime it returns nothing - a "void" function). It usually does a single task... and the caller doesn't need to know how it does it... just the interface (parameters & result) to be able to call it
int myFunction(int x, char y)
In the above example it expects 2 inputs x (an int) and y(a char), and provides a result that is type int.
If you avoid using global variables within the function, then the code is completely standalone... you could copy the exact function to another program and it will work without any modifications. You have encapsulated the logic within the function.
You actually called the function twice here. On the first line (after the comment) the function is called to calculate the sum, but then the returned result is simply thrown away! On the third line, the function is called again, repeats the same calculations over again, but at least this time the returned result is printed to serial monitor.
This would avoid that problem:
// Sum of the first three folders (96+106+229 = 431).
int s = resultShouldBe();
Serial.print("Result = ");
Serial.println(s);
but don't get confused about using the variable 's' here. It's a completely different variable to the one inside resultShouldBe(), even though they share the same name.
Thanks, thatās helpful. Iām sure Iāll get more comfortable with variable scope and returning functions with further practice. And reading; I have Simon Monkās excellent āProgramming Arduinoā at hand as I write!
reading everything was not the point, having the full code as part of your first post, you would have had the answer right away. This is such a common newbiesā mistake that this would be the first thing we looked at - as my post #4 hinted and you ignoredā¦
(Itās OK to reuse variableās name in different function, it is not an error)
My point was that if you had attached the code to your first post as is recommended, you would likely have had the answer in post 2⦠(and we would not be discussing this 30 post later )