简体   繁体   中英

I have a warning and error with void value; warning:in function returning void, error: void value not ignored

My code is getting these warnings and I'm not sure how to fix them. Warning: 'return' with a value, in function returning void [enabled by default] Error: void value not ignored as it ought to be

Here is some of my code:

void print_month(int month, int year, int first_day, int year_view){
  int width, row, col;
  int days = days_per_month[month - 1];

  if (month == 2){
    days += leap_year_correction(year);
  }

  --month;

  if (year_view){
    row = (month /3) * 9 +2;
    col = (month % 3) * 22;
  } else{
    row = col = 0;
  }
  if (year_view){
    width = 10 + strlen(month_names[month])/2;
    sprintf(grid[row] + col, "%*s", width, month_names[month]);
  } else{
    width = 8 + strlen(month_names[month])/2;
    sprintf(grid[row] + col, "%*s %d", width, month_names[month], year);
  }
  sprintf(grid[row + 1] + col, "Su Mo Tu We Th Fr Sa");
  sprintf(grid[row + 2] + col, "%*s", 3 * first_day, "");
  row += 2;
  for (int i=1; i <= days; i++){
    sprintf(grid[row] + col + 3 * first_day, "%2d", i);
    if ((first_day = (first_day + 1) % 7) == 0){
      row += 1;
    }
  }
  return first_day;
}

The next part is code from my main program

first_day = find_first_day(month < 0 ? 1 : month, year);
  if (month < 0){
    sprintf(grid[0] + 30, "%d", year);
    for (int i=1; i <= 12; i++){
      first_day = print_month(i, year, first_day, 1);
    }
  } else{
    print_month(month, year, first_day, 0);
  }
  print_grid(month < 0);
  return 0;

void print_month(...) means this function will return no values. But you are returning a value at the end - return first_day;

Change void print_month(...) to int print_month(...) to make it return an integer value.

The print_month function is declared with a return type of void , which means it returns no value. That's why you get a warning when you try to return a value from the function and and warning when you try to use the function's return value.

You want this function to return a value of type int , so change the return type to that:

int print_month(int month, int year, int first_day, int year_view){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM