Meaning of 'void' doesn't return a value

Hi all, I recently came across an answer of the meaning of 'void' on this forum. It was claimed using this doesn't return a value to the main program.
Check this programming out;
In the main program; read_sensors();
Serial.println(t);
out of the main program; void read_sensors(){
The void part reads a temperature. The temp reads out in the print statement.
This negates the claim that 'void' doesn't return a value. I am sorry but I am not going to give you the entire program as then I would need too much explaining of how the program works. I have neither the time or inclination to do this.
If somebody could explain this on this limited coding that would be great.

This is not a claim, this is a fact. show the code where your function is defined

??????

The function could be updating some global variable so it doesn’t have to return anything, without the code which you are so reluctant to show, have fun poking in the dark

When you call the so called void function, that function does not return a value when that function is finished executing.


The function below is a so called int function. :crazy_face:
This function returns an integer when it is finished executing.

int sum = addTwoNumbers( 4, 12 );
. . .
int addTwoNumbers(int A, int B )
{
return (A + B);
}

sum will then be set to 16.

Hello petercl14
Take a view here.

Have a nice day and enjoy coding in C++.

1 Like

me too!

" "When I use a word," Humpty Dumpty said, in rather a scornful tone, "it means just what I choose it to mean—neither more nor less. " "

(Martin Gardner's "Annotated Alice" was on the reading list for my computer science course, and my ragged, dog-eared copy still sits on my bookshelf)

The keyword "return" has specific meaning in C++, and no amount of your bluster and waffle is going to change that definition.

2 Likes

The void function has the "side-effect" of printing a value, but it does not return a value.

Ideally, you create the minimal runnable code sample which demonstrates the issue. If you attempt that, it will become clear to you which mechanism is being used to convey the sensor data from the function into the main program. How to create a Minimal, Reproducible Example - Help Center - Stack Overflow

A function returning something is quite specific. If your function was returning the value t then the call would be t = read_sensors().

So the function is not returning anything, instead it is probably setting or modifying the value of a global variable t.

Steve

following errors result from code below

C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In function 'void setup()':
Tst:7:19: error: void value not ignored as it ought to be
     int x = func ();
                   ^
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:7:9: warning: unused variable 'x' [-Wunused-variable]
     int x = func ();
         ^
exit status 1
void value not ignored as it ought to be
void
func (void)
{
}

void setup() {
    int x = func ();
}

void loop() {
}

Nope, we don't care how it works. We just need to know the problem & where it is.

In this case you have confused a return with an action. In a function if you put 'int a = b + c;' , this is an action, as is 'Serial.println("I know the answer is 42!");'. A funtion that returns a value will use the 'return' word like 'return c;' . This can then be assigned (or ignored) to a variable.

Either t is a global variable or your code is incorrect. So with a global variable, read_sensors() can be something like

void read_sensors()
{
  t = analogRead(A0);
}

and will set the global variable t, it will not return a value.

If your program is properly written (and documented), a lot of us will be able to understand it. If it's badly written, it will eventually bite you; in a few months time (e.g. when a bug-fix is needed or you want to add a new feature) you will have no idea why you were doing what you were doing.

My float boat sank. :crazy_face:
Wish Mr. Ritchey had used another word like "empty" for a function that doesn't return a value, then the noobs could say "my empty won't compile".

A sensible programmer would call the function "readTemperature".

Just sayin'

(The "void part" reads nothing; it informs the compiler)

I wish that he had used the word elephant, then they could say "my elephant won't compile"

Hi,
My principles are to avoid making fun of people with less knowledge than mine.
But I also think people should respect and listen to more experienced people.

You can question, debate, but ignoring the acquired knowledge is not pleasant.

I thought you weren't heard in the post:

But seeing your current post, I come to think that I made a mistake in asking that question.

void is not a function;
A function return requires the "return" command in the function;
Obtaining a value through a global variable does not mean function return.


int void a ()
{
Serial.print("ojdfgvodnfbolk")'
}

If the code prints the serial prints "ojdfgvodnfbolk" the function has not returned a value.

On the other hand


int void a ()
{
return 10;
Serial.print("ojdfgvodnfbolk")'
}

returns a value.

but you are saying that this


void a ()
{
return 10;
}

works?

No, it was appropriate.

I can’t say I knew of those other posts, or remembered having slotted the OP into the ignore/troll category, and even if I had, should have waited for the (still waiting for it) sketch that would have demonstrated the problem.

As my mother never would have told me, if you haven’t something nice to say, might as well not. Say anything.

Yes, I just threw my Moms, may she RIP, right under the bus.

a7

@petercl14, I really need your help in figuring out why my code is not returning a value but your code does return a value.

LOL, I remember (or do I?) when a function that returned nothing was just sitting there, no void keyword.

I remember (or do I?) using some time up searching through a bunch of source code only to find that void meant nothing, in the way it does.

So I wonder when that keyword was introduced, and by whom.

# include <stdio.h>

main(){
    printf("Hello World!\n");
}

a7