A theoretical question

Hi,
I was wondering why we have to use the pinMode declaration in sketches. If you use an analogWrite or digitalWrite command for a specific pin, doesn't it imply that particular pin is an output? And if you use the read versions of the similar command, doesn't it imply the opposite?

Thanks

doesn't it imply that particular pin is an output?

Using analogWrite(), yes. Using digitalWrite(), no. You might be turning the pullup resistor on for an INPUT pin.

And if you use the read versions of the similar command, doesn't it imply the opposite?

Pins default to INPUT, so read generally works without the need to use pinMode(). In fact, though, you can read the state of an OUTPUT pin. So, digitalRead() rells you nothing about the state of a pin.

If you study the Atmega 328 datasheet you will see that the mode of a pin is separate from the question of whether you want to read or write the pin. I find that section of the datasheet a bit confusing (forgive the pun).

...R

...doesn't it imply...

May well be true, but basing code on implication is almost never a good idea. Explicitly using statements, if nothing else, documents what you want to do.