pinMode()

int a=7;

I've been using pinMode to define the state of the led as well instead of digital write but still it functions. I understand this is not the standard way to do so but still it works, can anyone please explain me why???

int b=13;
void setup() {
// put your setup code here, to run once:
pinMode(b,OUTPUT);
pinMode(a, INPUT);

}

void loop() {
int x=digitalRead(a);
if (x==1){
pinMode(13, LOW);
}
else{
pinMode(13, HIGH);
}
}

check what HIGH and LOW values are defined to, then check what INPUT and OUTPUT are then read about INPUT_PULLUP (and what happens when a pin set as INPUT is taken HIGH). And then imagine what your code does. You’ll probably find out what’s happening

It’s not that this is not the standard way, it is the WRONG way…and you’ll have to define the statement “it works”… (it sure does something!)

Please use code tags to post code

It depends on the board what will happen when using your code. An Uno drives the led/resistor using an opamp. Other boards (e.g. Nano) drive the led/resistor directly.

I've been using pinMode to define the state of the led

No, you haven't. You use pinMode to define the MODE of a pin.

It "works" (or seems to) because LOW == 0 == INPUT and HIGH == 1 == OUTPUT. Using the wrong name doesn't change the way that pinMode() works. Depending on the state of the input pin you are setting the LED pin as an INPUT or an OUTPUT.

When you set the LED pin as an OUTPUT it drives the LED to whatever level the output pin happens to be set for. That could be HIGH or LOW depending on how that uninitialized pin has defaulted. I suspect it defaults to LOW.

When you set the LED pin as an INPUT it leaves the pin floating. It may be floating HIGH or it may be floating LOW. I don't think you can rely on either state.

It may appear to "work" but it could stop working at any time if the input floats in a different direction.