what is wrong with this coding. trying to use a subroutine.
float time;
float lt;
void setup() {
Serial.begin(115200);
}
void loop() {
time(); // time is name of subroutine
lt;
Serial.println(lt);
}
time(){
lt=5;
return lt;
}
what is wrong with this coding. trying to use a subroutine.
float time;
float lt;
void setup() {
Serial.begin(115200);
}
void loop() {
time(); // time is name of subroutine
lt;
Serial.println(lt);
}
time(){
lt=5;
return lt;
}
Please post the errors (also using code tags).
does not do anything.
You have to specify the return type.
Your time() function does not have a return data type specified
Note too that
lt;
does nothing in the sketch and nor does
a1;
In your other topic
Also, you cannot have a variable with the same name as a function
// float time;
float lt;
void setup() {
Serial.begin(115200);
}
void loop() {
time(); // time is name of subroutine
// lt;
Serial.println(lt);
}
float
time (){
lt=5;
return lt;
}
time both as a variable and a functiontime()lt; does nothingNot much, mainly because it's just a dozen of lines.. ![]()
First things first. Indentation: to make the code more readable, use indentation. It means you "nest" the code based on what "contains" it. Usign the IDe, pressing Ctrl-T it will do the job for you, changing your code to look like this (I have also removed some empty lines and added one to separate the blocks):
float time;
float lt;
void setup() {
Serial.begin(115200);
}
void loop() {
time(); // time is name of subroutine
lt;
Serial.println(lt);
}
time(){
lt=5;
return lt;
}
But it's still a wrong code, as I haven't made any changes.
First of all in C there aint "subroutines", we have "functions". A "subroutine" is a code section doing "something", a "function" is a code section doing something and "return a value". In your case, "time()" is a function returning that "lt" variable value. Please note also "setup()" and "loop()" are functions, without a return value (that's why they're marked "void").
But also the "time()" function must declare what it returns, in your case is a float value so the declaration should be:
float time() {
lt=5;
return lt;
}
With your first line "float time;" you defined a global variable called "time", not the value returned by the function "time()", and must be removed. And the "float lt;" instruction creates an "lt" global variable, the one you used inside the "time()" function, assigning a value, then printed on the serial port.
Lastly, the "lt;" instruction you have in your loop() does nothing, so it can be removed, and it's also a good practice adding some comments to specify meaning and usage of some sections or variables.
The final code coudl be like this:
float lt;
void setup() {
Serial.begin(115200);
}
void loop() {
time();
Serial.println(lt);
}
float time(){
lt=5;
return lt;
}
Obviously it's a code doing nothing except continuously printing "5" over the serial (the "loop()" function is automatically called repeatedly thousands of times per second). If you're just practicing (to learn the C language) you could just put your code just in the "setup()" function, so it runs once on start:
float lt;
void setup() {
Serial.begin(115200);
// Test code
time();
Serial.println(lt);
// or just:
// Serial.println(time());
}
void loop() {
}
float time() {
lt=5;
return lt;
}
This is the only actual problem that I can see. The rest is just terrible coding practice, but besides that, it should run.
What about the missing return data type on the time() function ?
Compiler will default to int and give a warning. Bad practice, but it should run.
In that case why doesn't this compile ?
float lt;
void setup() {
Serial.begin(115200);
}
void loop() {
time();
Serial.println(lt);
}
time(){
lt=5;
return lt;
}
Looks like it should. What errors does it give?
Having just explained this in another topic recently, I knew the answer and here's the relevant bits of the error output:
error: expected constructor, destructor, or type conversion before ';' token
time(){
^
And with -Wall you also get:
error: 'time' was not declared in this scope
time();
^~~~
warning: ISO C++ forbids declaration of 'time' with no type [-fpermissive]
time(){
^
Odd. I expected the warning, but not the error. Did you change your default -W settings?
This is the error with Compiler Warnings to None in the IDE but that does not matter.
Compiling sketch...
"C:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino" "-IC:\\Users\\bugge\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard" "C:\\Users\\bugge\\AppData\\Local\\arduino\\sketches\\D34F94F035111CC43AF4417A71441715\\sketch\\sketch_jan24a.ino.cpp" -o "C:\\Users\\bugge\\AppData\\Local\\arduino\\sketches\\D34F94F035111CC43AF4417A71441715\\sketch\\sketch_jan24a.ino.cpp.o"
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025024-18716-ct52ok.9wk6\sketch_jan24a\sketch_jan24a.ino:12:8: error: expected constructor, destructor, or type conversion before ';' token
time(){
^
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025024-18716-ct52ok.9wk6\sketch_jan24a\sketch_jan24a.ino: In function 'void loop()':
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025024-18716-ct52ok.9wk6\sketch_jan24a\sketch_jan24a.ino:8:3: error: 'time' was not declared in this scope
time();
^~~~
C:\Users\bugge\AppData\Local\Temp\.arduinoIDE-unsaved2025024-18716-ct52ok.9wk6\sketch_jan24a\sketch_jan24a.ino:8:3: note: suggested alternative: 'tone'
time();
^~~~
tone
exit status 1
Compilation error: expected constructor, destructor, or type conversion before ';' token
It refers to the time() function definition.
PS
I normally have the Compiler Warnings set to All.
It's an error, at least in the 21st century.
You may be old, so old as to have programmed in C at a time when you could leave off the type and it would in fact default to int.
There was no void keyword, so I guess ppl just didn't use return and never used the not-returned int?
Sounds like enough for some really nasty coding problems...
a7
I know that code if it worked would give me repeated 5s. I have reduced the actual code and subroutine to a basic very simple code to find the error in my original more complicated code.
For all the comments on this no one has actually come up with a code which actually compiles. I keep getting errors in my compiling some of which don't make much sense.
I have code that compiled ok on my nano board. This still works as it should. I did not keep a copy of this code. I made a few changes to the code which now does not compile.
The error is here in this code because it does not compile.
If one of you guys can correct the code I have given so that it compiles that would be great. Please show the code exactly. You will have tested it to see that it compiles. You will in the program get repeated 5s printed out.
I have been looking at the possibility of recovering my working code from the nano. It may be possible but very difficult.
This is a warning to me and you that if you have a good working code make a copy of it. If you do then you wont be having the problems I now have.
Post #4.
Are you serious? See post #4, and my post #5 where I described everything that was wrong, and I also gave you the code at the end that you could try to use!
Please always read all the comments/posts before, if you miss to do it you'll just get no more answers.
It's a difference between C & C++. Or at least the versions I have installed.
e.g., the following compiles in avr-gcc but not avr-g++
volatile int v = 0;
int main(void)
{
int i = 3;
test1(i);
return 0;
}
test1(int a)
{
v = a+1;
}
Yes, THX. I see only warnings.
Something I read led me to believe that since C99 it turned into an error. But I can see that would make trouble.
a7
You will have read previous replies and do your own work.