Why can't I assign a certain digitalWrite statement to a variable?

Hello, I'm trying to assign digitalWrite(ledPin, HIGH); to ledOn but it's not working. Any way to do it?

How do I make something like this not give me an error message?

ledOn = digitalWrite(ledPin, HIGH);

Why do you want to do that? In other words, what is it that you actually want to accomplish?

What are you trying to do?

Make ledOn a function?

void ledOn()
{
  digitalWrite(ledPin, HIGH);
}

Call with

ledOn();

@jremington @larryd I'm just messing around and trying to learn how the Arduino code works together by turning on a simple LED in different ways.

@groundFungus Thank you so much; making a function didn't even occur to me. Worked out like I wanted to.

Works perfect for what I wanted to do:

 int ledPin = 13;
int buttonPin = 2;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void ledOn() {
  digitalWrite(ledPin, HIGH);
}
void loop() {
  if (digitalRead(buttonPin)) {
    ledOn();
  }
    else {
      digitalWrite(ledPin, LOW);
    }
  }

You can always use a macro.

#define ledOn digitalWrite(13, 1)

then use ledOn; in your code. Augment it with,

#define ledOff digitalWrite(13,0)

@DKWatson WOW, my mind is blown. That is very nifty. Thank you for the lesson.

digitalWrite does a thing - it sets a pin to a value. It doesn't itself have a value - its return type is 'void'.

Are you trying to set ledOn to the value that the pin was set to? Well, since you just told digitalWrite what to set that pin to, surely you already know this?

PaulMurrayCbr:
digitalWrite does a thing - it sets a pin to a value. It doesn't itself have a value - its return type is 'void'.

I think this made it click; thanks.