If void() does not return a value, why do we use i

2019-01-21 15:16发布

问题:

void f() means that f returns nothing. If void returns nothing, then why we use it? What is the main purpose of void?

回答1:

When C was invented the convention was that, if you didn't specify the return type, the compiler automatically inferred that you wanted to return an int (and the same holds for parameters).

But often you write functions that do stuff and don't need to return anything (think e.g. about a function that just prints something on the screen); for this reason, it was decided that, to specify that you don't want to return anything at all, you have to use the void keyword as "return type".


Keep in mind that void serves also other purposes; in particular:

  • if you specify it as the list of parameters to a functions, it means that the function takes no parameters; this was needed in C, because a function declaration without parameters meant to the compiler that the parameter list was simply left unspecified. In C++ this is no longer needed, since an empty parameters list means that no parameter is allowed for the function;

  • void also has an important role in pointers; void * (and its variations) means "pointer to something left unspecified". This is useful if you have to write functions that must store/pass pointers around without actually using them (only at the end, to actually use the pointer, a cast to the appropriate type is needed).

  • also, a cast to (void) is often used to mark a value as deliberately unused, suppressing compiler warnings.

    int somefunction(int a, int b, int c)
    {
        (void)c; // c is reserved for future usage, kill the "unused parameter" warning
        return a+b;
    }
    


回答2:

This question has to do with the history of the language: C++ borrowed from C, and C used to implicitly type everything untyped as int (as it turned out, it was a horrible idea). This included functions that were intended as procedures (recall that the difference between functions and procedures is that function invocations are expressions, while procedure invocations are statements). If I recall it correctly from reading the early C books, programmers used to patch this shortcoming with a #define:

#define void int

This convention has later been adopted in the C standard, and the void keyword has been introduced to denote functions that are intended as procedures. This was very helpful, because the compiler could now check if your code is using a return value from a function that wasn't intended to return anything, and to warn you about functions that should return but let the control run off the end instead.



回答3:

void() means return nothing.

void doesn't mean nothing. void is a type to represent nothing. That is a subtle difference : the representation is still required, even though it represents nothing.

This type is used as function's return type which returns nothing. This is also used to represent generic data, when it is used as void*. So it sounds amusing that while void represents nothing, void* represents everything!



回答4:

In imperative programming languages such as C, C++, Java, etc., functions and methods of type void are used for their side effects. They do not produce a meaningful value to return, but they influence the program state in one of many possible ways. E.g., the exit function in C returns no value, but it has the side effect of aborting the application. Another example, a C++ class may have a void method that changes the value of its instance variables.



回答5:

Because sometimes you dont need a return value. That's why we use it.



回答6:

If you didn't have void, how would you tell the compiler that a function doesn't return a value?



回答7:

Cause consider some situations where you may have to do some calculation on global variables and put results in global variable or you want to print something depending on arguments , etc.. In these situations you can use the method which dont return value.. i.e.. void



回答8:

Here's an example function:

struct SVeryBigStruct
{
    // a lot of data here
};

SVeryBigStruct foo()
{
    SVeryBigStruct bar;
    // calculate something here

    return bar;
}

And now here's another function:

void foo2(SVeryBigStruct& bar) // or SVeryBigStruct* pBar
{
    bar.member1 = ...
    bar.member2 = ...
}

The second function is faster, it doesn't have to copy whole struct.



回答9:

probably to tell the compiler " you dont need to push and pop all cpu-registers!"



回答10:

Sometimes it can be used to print something, rather than to return it. See http://en.wikipedia.org/wiki/Mutator_method#C_example for examples



回答11:

Functions are not required to return a value. To tell the compiler that a function does not return a value, a return type of void is used.



标签: c++ types void