syntax of "micros" vs "micros()"

I am new to coding in C and using the Arduino so this may be covered somewhere that the search failed to find.

While debugging a sketch using a bunch of timers to collect enough data to even analyze my issue I noticed that I had accidentally left the parentheses off of a micros() statement and it seemed to have compiled without complaint. I tried it both ways and there seemed to be no difference. The IDE recognizes "micros" and makes the text red so maybe at that point the compiler doesn't care whether you add the () or not since there is no parameter involved. But it did seem a little strange to not get a compile error for a statement that diverges from the specified syntax.

micros is the address of the function micros(). Functions always have () even if no parameters.

IDE recognising the name is not important as it is just matching names - there is no intelligence there. The compiler will allow this, however, as it is valid syntax - that is more important.

DocDough: The IDE recognizes "micros" and makes the text red so maybe at that point the compiler doesn't care

The colour changing is just a feature of the editor in the IDE. The complier is a seperate process to the editor that is only run when you ask it to, it pays no attention to the colours.

You can usually find the referance page for a command by doing a Google search on the command;

'Arduino reference micros'

Try the example there, it produces an error when the () is missing from micros.

When I tried the sample code with "micros" instead of "micros()", it compiled and ran without error.

Yes it will compile because it is valid syntax. But it doesn’t do anything useful. Without the parens you just get the memory address of the micros function. It doesn’t give you the microsecond count like it does with the parens.

I didn't notice that and it makes a difference, and that may be one reason why my timing analysis of the errant sketch was making me nuts.

Thanks for leading me to the water and making me drink!

If you'd turn up your compiler warnings (under File --> Preferences), you'd probably see one for the invalid conversion of a function pointer -- 'long unsigned int (*)()' -- into some other variable type.

I have compiler warning turned up fo I don't get stuck trying to diagnose a silly mistake. When i use micros; instead of micros(); I get this warning:

sketch_aug26a.ino: In function 'void setup()':
sketch_aug26a.ino:4:9: warning: statement is a reference, not call, to function 'micros' [-Waddress]
   micros;
         ^
sketch_aug26a.ino:4:9: warning: statement has no effect [-Wunused-value]

It is unusual to call micros() without doing something with the result. If I use "unsigned long time = micros;" instead of "unsigned long time = micros();" I get the following error:

sketch_aug26a.ino: In function 'void setup()':
sketch_aug26a:4:24: error: invalid conversion from 'long unsigned int (*)()' to 'long unsigned int' [-fpermissive]
   unsigned long time = micros;
                        ^

Thanks for the coaching. I was looking for the switch and had not yet found it. I am sure there are lots of things I have not yet discovered. Somewhat painful, but also educational.

It would be nice if the language had a keyword to declare a function that has no side-effects. Then the compiler could warn on statements like "digitalRead(pin);" where a function returns a value but has no side effects and the returned value is not used.

johnwasser: It would be nice if the language had a keyword to declare a function that has no side-effects. Then the compiler could warn on statements like "digitalRead(pin);" where a function returns a value but has no side effects and the returned value is not used.

g++ has an extension that you can put before a function declaration:

__attribute__ ((pure)) void foo(void);

But yes, it would be nice to have a "pure" keyword in standard C++. Maybe there will be in C++20....

christop: g++ has an extension that you can put before a function declaration:

__attribute__ ((pure)) void foo(void);

But yes, it would be nice to have a "pure" keyword in standard C++. Maybe there will be in C++20....

but side effect is what make up imperative languages though.

arduino_new: but side effect is what make up imperative languages though.

I don't understand your point. I hoped to warn beginners when they make the common beginner mistake of calling a function that only returns a value (has no side effects) without using the returned value.

__attribute__((warn_unused_result)) is what you are looking for.

johnwasser: I don't understand your point. I hoped to warn beginners when they make the common beginner mistake of calling a function that only returns a value (has no side effects) without using the returned value.

My point is it's not possible to write a no-side-effect function/program.

arduino_new: My point is it's not possible to write a no-side-effect function/program.

But it's possible to write individual functions that have no side effects, such as digitalRead().

christop: But it's possible to write individual functions that have no side effects, such as digitalRead().

digitalRead() returns a value, which is a side effect

arduino_new: digitalRead() returns a value, which is a side effect

OK, now I understand. Your definition of 'side effect' differs from the definition in Wikipedia.

https://en.wikipedia.org/wiki/Side_effect_(computer_science)

In computer science, an operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside its local environment, that is to say has an observable effect besides returning a value (the main effect) to the invoker of the operation.