Home > Software design >  error: void value not ignored as it ought to be in Arduino
error: void value not ignored as it ought to be in Arduino

Time:12-05

This is a function to convert 1 digit number to 3 digit. For example convert '2' to '002'.

void loop() {
 int x = convertdigit(time);
}

void convertdigit(int num){
  char buffer[50];
  int n;
  n=sprintf (buffer, "d",num);
  return buffer;
}

Error: void value not ignored as it ought to be

/sketch/sketch.ino: In function 'void loop()':
/sketch/sketch.ino:33:30: error: void value not ignored as it ought to be
     int x = convertdigit(time);
                              ^
Error during build: exit status 1

May i know how to fix it?

CodePudding user response:

When you write this you are telling the compiler that convertdigit does not return anything (void):

void convertdigit(int num)

When you write this, you are telling the compiler to use the return value of convertdigit and store it in x:

int x = convertdigit(num);

Those two things are in conflict: if convertdigit doesn't return anything, how can you store that nothing in x? Your code is kind of confusing so I'm not sure what you actually intended, but now that I have explained that error message, I hope you are able to make progress.

Hint: If you want convertdigit to return an int, change void convertdigit(... to int convertdigit(....

  • Related