Meaning of 'void' doesn't return a value

Where would main() return a value to even if it could ?

Interestingly, the Arduino main() function has a return type of int

Another baseless claim in another useless post.

They say you're supposed to say something nice, sooooo: OP, you are quite excellent at wasting everyone's time.

1 Like

K&R (PBUT) edition one has "main" in the "hello world" program without a type.

It doesn't have a "#include <stdio.h>" either, but the second edition does, though still no type for "main"

Wasn't no return type specified for a function implicitly int at that time?

1 Like

I don't remember, but the "hello world" on page 6 doesn't have a return, or an exit().
Does that mean an implicit return of zero?

I don't think so, I would guess that it meant grab whatever is hanging around on the stack and return that.

I no longer have access to a compiler of that vintage to try it though.

????

#include <stdio.h>

main ()
{
    printf ("hello world\n");
}
_Others vi main.c; make main && main
cc  -I ../Include -I /tools/Arduino/hardware/arduino/avr/variants/standard -I DS3231-1.0.2   main.c   -o main
main.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
    3 | main ()
      | ^~~~
hello world

and

_Others mv main.c main.cpp
_Others rm main.exe
_Others make main && main
g++  -I ../Include -I /tools/Arduino/hardware/arduino/avr/variants/standard -I DS3231-1.0.2   main.cpp   -o main
hello world

@gcjr, I'm not sure what the "????" is for

True K&R C, and the famous K&R book, in the mid-'70s, contain no mention of the void keyword. It did not yet exist. The addition of void to the language came MUCH later, in the very early days of ANSI C, in the late '80s. In fact, true K&R C had no way to define a function that had no return value, nor did it even support function prototypes.

Yes, exactly. There was no such thing as void, in ANY context.

C didn't even have requirements for the proper number of arguments, much less that they were the correct type.

not sure this answers your very valid question.

the lack of a requiring a return value for a non-void function may only be an error with the -Werror option

_Others cc -Werror main.c
main.c:3:1: error: return type defaults to ‘int’ [-Werror=implicit-int]
    3 | main ()
      | ^~~~
cc1: all warnings being treated as errors

Can’t say that isn’t true, but void was in use before solid official standards made it part of the language.

I remember (or do I?) finding could it have been as simple as

 # define void

somewhere when I was trying to figure out WTF it meant.

The beginning of making C a little less unfriendly.

a7

I would say you are correct. My program has to read a sensor regularly. The sensor reading is in itself a fairly long program. So one solution is to include the sensor reading in the main program which would require a lot more programming.
If I was using the 'basic' language I would 'gosub' every time I wanted to read the sensor. So now in Arduino I can't use 'void' as this is not returning a value.
When I came across the instruction read_sensors(). somewhere I thought this included 'void' in a sub-routine but I must have been wrong.
Can you suggest a way to do the coding so that I can call on a subroutine to return the sensor reading value similar to the way I would have done it in 'basic' coding?
I guess what I am asking is what goes with read_sensors() if you can't use 'void' in a subroutine.

In this example, I assume that read_sensors() returns an integer.

const uint8_t sensorPin = A0;
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  int sensorReading = read_sensors();

  Serial.println(sensorReading);
}

/*
  read sensor
  Returns:
    sensor reading
*/
int read_sensors()
{
  int x = analogRead(sensorPin);
  return x;
}

Because you've named the function read_sensors(), there might be multiple sensors that you read in that function.; in that case, be aware that you can't return multiple values. The solution is to pass references / or pointers to the variables where you want to store the values as arguments to the function. The below uses references.

const uint8_t sensorPin0 = A0;
const uint8_t sensorPin1 = A1;
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  int sensorReading0;
  int sensorReading1;
 read_sensors(sensorReading0, sensorReading1);

  Serial.println(sensorReading0);
  Serial.println(sensorReading1);
}

/*
  read sensors
  In:
    references to the variables where the result must be stored
  Returns:
    nothing
*/
void read_sensors(int &val1, int &val2)
{
  val1 = analogRead(sensorPin0);
  val2 = analogRead(sensorPin1);
}

The example uses integers for both variables, you can also use an int and a float or whatever.

For a tailored advise, please post your read_sensors() (I doubt that there will be any patented stuff in there) and a simple example how you use it.

Not much guidance at all in Arduino on this subject. I was trying to find the equivalent of BASIC 'gosub' statement. Here is how I finally did it.
In the main program; double t=read_sensors();
Outside the main program; double read_sensors(){ in here is my program to read a sensor. At the end of the program still in the curvy brackets; return t;}
If you substitute anything but read_sensors() here you get a compiler error 'not defined'.
I only found read_sensors() by accident on the internet. There is nothing about this instruction in 'help' in Arduino. It would certainly help users if Arduino could include in their help column how to use subroutines. I have to read a sensor regularly in my program and you don't want to have to repeat this over and over in your main program. You would be using a lot of program space.

@petercl14 topics merged

Why did you start a new topic when this subject was already being covered in this one ?

What you are describing is basic C coding. The Arduino help pages do not attempt to describe C language because there are many tutorials and manuals available on-line.

The ability to call a function and have it return a value has nothing to do with the Arduino environment, rather it is standard C functionality

The Arduino help, examples and reference cannot be expected to cover the whole of the C and C++ functionality, particularly as there are literally thousand of online C and C++ tutorials available

Just idle curiosity on my part, but what did you think pinMode, digitalWrite et al were?

(My recollection of BASIC is that all subroutines were void, and that a RETURN was essential)

1 Like

I assume you know this but for those who don't ... The return value from main becomes the exit code for the process. Obviously, that doesn't answer the question for avr-libc.

main is called here. When main returns execution passes to exit which is expected to never return. exit disables interrupts and loops forever. (Putting the processor to sleep seems like a better choice.)

The return value is ignored.

Probably because that's a requirement to be C or C++ standards compliant.