What is the purpose of functions which return void?

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

Upvotes: 18

Views: 52923

Answers (11)

Zain
Zain

Reputation: 43

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.

Upvotes: 0

Mateusz N
Mateusz N

Reputation: 11

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.

Upvotes: 1

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361472

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!

Upvotes: 8

waldrumpus
waldrumpus

Reputation: 2590

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.

Upvotes: 7

Matteo Italia
Matteo Italia

Reputation: 126797

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;
    }
    

Upvotes: 37

Vins
Vins

Reputation: 4119

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

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726589

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.

Upvotes: 7

Tech163
Tech163

Reputation: 4286

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

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

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

Upvotes: 1

Rango
Rango

Reputation: 1105

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

Upvotes: 3

huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11920

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

Upvotes: 0

Related Questions