I suppose it would have been entirely legitimate for the designers of C/C++ to simply say that if a function is defined with no return value, it returns no value. I.e., by way of example,
thisfunctionreturnsnothing()
{
/* do something */
return;
}
vs.
int thisfunctionreturnanint()
{
/* do something */
return 123;
}
However, don't forget that C/C++ has history. Back in the 1980s, pre-standard C (sometimes called K&R C, where K&R refers to the names of Kernighan and Ritchie, who created the language and wrote the first C language manual) allowed function declarations like this one:
thisfunctionreturnssomethingthesizeofanint()
{
/* do something */
return 456;
}
So even though the function had no return type, it was allowed to return anything that had the same number of bytes as an int on a given implementation. (For instance, if a 16-bit machine had 16-bit integers and 16-bit pointers, it was permissible to return either an int or a pointer in such a function.)
When the C standard was created, these old-style declarations were kept for backwards compatibility with existing programs. (As a matter of fact, even modern compilers tend to accept old-style function declarations, albeit with warnings or only when appropriate command-line switches are used.) So the void type was created explicitly to distinguish this:
void thisfunctionreturnsnothing()
{
/* do something */
return;
}
from this:
thisoldstylefunctionmayreturnsomething()
{
/* do something */
return 123;
}
I hope this really explains why things evolved the way did, even though we would likely do things somewhat differently if the C/C++ language was created from scratch today.
To put it simply, there are generally two types of "subroutines":
1. Functions - they may or may not take in arguments, they do some work (with or without those arguments) and then finally, they return a value (single, list, complex object). They return something to the point where they are called.
2. Procedures - they may or may not take in arguments, they do some work (with or without those arguments) and they end. The program continues where it had stopped before the procedure was executed.
Now, if you do not see how or when you might use procedures, than that is a whole different problem and will certainly become clear in time with experience.
Now, as for void in C/C++ languages, as Peter had explained above, can be used to define pointers to memory locations regardless of the kind (type of) data stored at that location. This is useful, but is also very "dangerous" as in, your program may crash if you do not know what you are doing and/or if you do not predict possible situations that may happen at runtime.
Void is a "return type" for subroutines that do not return anything, meaning it is a way to define procedures in C/C++/Java...
1. A return type of void allows you to define a function that does not return a value. Note that it is NOT the same as returning 0. The value of 0 is of type integer, float, double, etc; it is not a void. (In other languages, a function with no return value may be called a "subroutine" or "procedure", whereas a "function" always returns something. In C/C++, they are all called functions.)
2. A pointer to void is a generic pointer that can be used when the type of the data at the location is unknown. So you can use a type of void * to refer to an address in memory without knowing what is actually located there.
A very good book that provides a lot of background on the design decisions of the C++ language is "The Annotated C++ Reference Manual" by Ellis and Stroustrup.
I suppose it would have been entirely legitimate for the designers of C/C++ to simply say that if a function is defined with no return value, it returns no value. I.e., by way of example,
thisfunctionreturnsnothing()
{
/* do something */
return;
}
vs.
int thisfunctionreturnanint()
{
/* do something */
return 123;
}
However, don't forget that C/C++ has history. Back in the 1980s, pre-standard C (sometimes called K&R C, where K&R refers to the names of Kernighan and Ritchie, who created the language and wrote the first C language manual) allowed function declarations like this one:
thisfunctionreturnssomethingthesizeofanint()
{
/* do something */
return 456;
}
So even though the function had no return type, it was allowed to return anything that had the same number of bytes as an int on a given implementation. (For instance, if a 16-bit machine had 16-bit integers and 16-bit pointers, it was permissible to return either an int or a pointer in such a function.)
When the C standard was created, these old-style declarations were kept for backwards compatibility with existing programs. (As a matter of fact, even modern compilers tend to accept old-style function declarations, albeit with warnings or only when appropriate command-line switches are used.) So the void type was created explicitly to distinguish this:
void thisfunctionreturnsnothing()
{
/* do something */
return;
}
from this:
thisoldstylefunctionmayreturnsomething()
{
/* do something */
return 123;
}
I hope this really explains why things evolved the way did, even though we would likely do things somewhat differently if the C/C++ language was created from scratch today.
Purpose? Simpler: the evaluation of an expression have two purposes (1.9p12):
(References are extracted from the C++ Standard working draft n3337, reachable here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf)
- Value computation
- Side effects (side effects are described in the same paragraph 1.9p12).
The purpose of expressions with non-void type is to compute some value (and possibily other side effects), and the purpose of void expressions are purely for their side effects. So, a void function is a way of encapsulating a set of side effects, in a "procedure-way".
The type void has an empty set of values (3.9.1p9). So, in a construct like `return expr;`, there is no `expr` which type fits the desired return type, except another void expression. Situations where void expressions can appear are described also in 3.9.1p9.
Basically Void is used with those Functions which will not return any value. Basically these Functions are used in Menu or for formatting of the applications like screens’ Borders etc (These types of Functions in other languages are known as the Procedures like in Pascal). People said that C/C++ is the language of Functions, in my point of view its wrong because the definition of Function is “ Every Function must Return any value” but in C/C++ when we put Void with the Function then its mean No Value will be returned by the Function which is the contradiction of the Function Definition.
Sir(Muhammad Sharif ) I agree with ur this statement ..
"People said that C/C++ is the language of Functions, in my point of view its wrong because the definition of Function is “ Every Function must Return any value” but in C/C++ when we put Void with the Function then its mean No Value will be returned by the Function which is the contradiction of the Function Definition."
It is very rare to see a C++ programmer use the words procedure or subprogram, this will vary from language to language. In many programming languages the word "Function" is reserved for subroutines that return a value, this is not the case with C++.