C Programming

Associate
Joined
28 Jul 2003
Posts
1,987
Location
The Moon
Hey people I am new to C and was wondering what does the term "void" actually mean / do? I see lots of programs with this in but have no idea what it does. I have searched on google but it has not clarified it for me.
 
Hey thanks for the help. I still am at odds to what it does specifically, a lot of that made no sense to me. I am awful when it comes to computers.
 
This is in the wrong forum for starters, needs to be moved to "HTML, Graphics & Programming". Then you might get a few more replies ;)

Void just means nothing, take for example a function with the signature:
Code:
void doSomething(int a)
The function returns nothing. Whereas if you had a function with the signature:
Code:
int doSomething(int a)
It would have to return an integer.

Hope that helps a bit? :o
 
It just means that the function does not return a result, the computation does not produce a value back to the calling function.
 
SoSolid said:
Again....what is the point of asking c to do something and then not returning a result!?
A function doesn't have to return something for it to be useful. Most of the time when you want a function to return a value it's so you can work out whether whatever the function was supposed to do was a "success" or "failure".

You could have a function, for example, which just increments a counter that is stored globally (I'm over-simplifying the concept of passing variables by reference, etc).. that function doesn't need to return anything to the part of code that calls it.

Long story short: "void" is the word used to explicitly say that nothing is being returned from a function or passed to it. There are a variety of reasons why you might want to explicitly state that nothing is being returned from, or passed to a function.
 
Last edited:
Durzel said:
A function doesn't have to return something for it to be useful. Most of the time when you want a function to return a value it's so you can work out whether whatever the function was supposed to do was a "success" or "failure".

You could have a function, for example, which just increments a counter that is stored globally (I'm over-simplifying the concept of passing variables by reference, etc).. that function doesn't need to return anything to the part of code that calls it.

OK I think I get it. So, perhaps, if you had a part of a program the just performs a calculation but does not need to show the answer, then one could use a void?
 
You could have a void function that just performs some print statements. That doesn't need to return anything.

It's not about "showing" the answer, it's about bringing it back into the function that callled it.
 
I hate c. I am trying to make a program which calculates all the prime numbers up to 1000. I don't understand this void business at all.
 
SoSolid said:
I hate c. I am trying to make a program which calculates all the prime numbers up to 1000. I don't understand this void business at all.

sieve of eratosthenes. only basic C knowledge needed.
 
burnsy2023 said:
Learn Java, then you'll love C/C++

Burnsy

Not saying i hate c, i love it, but only after i'd tried java, haskell, ocam and antlr. just saying that maybe the oP hasn't tried any other languages so doesn't appreciate c as much as they should
 
C was created back in the 70's for programming telephone switches and it aint improved much since :). C programmers defend the arcane syntax staunchly and say it's more readable, personally I hate having to use it and prefer more verbose languages like vb.net.

Also worth noting that even though something is void(in vb this would be a sub rather than a function) you can still of course return something by setting the value of the arguments if they are reference types.
 
pinkaardvark said:
Also worth noting that even though something is void(in vb this would be a sub rather than a function) you can still of course return something by setting the value of the arguments if they are reference types.
Surely that wouldn't be returning something? That would be altering a variable passed into the function?
 
Welshy said:
Surely that wouldn't be returning something? That would be altering a variable passed into the function?

Effect is the same ie.

Sub A(or void for C heads) instantiates a variable, lets call it X and say it's an integer.

Sub A calls Sub B passing a reference(pointer for C heads) to X as an argument

Sub B modifies X and exits

X has a new value and Sub A can do something with it.

...

Now if you convert Sub B to a function(or non void function for C heads) and take away the argument.

You then call the function and it's return value would set X.

either way Sub A ends up with a modified X.

Of course using a function can make code more readable but there are times when you need to return more than one value of differing types and can't be arsed to created a class to use as a return type and a function only has a single return whereas your unlimited(well within reason) to the number of args you can pass.
 
Back
Top Bottom