What is the purpose of functions which return void?
Asked Answered
K

11

18

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

Katabasis answered 19/7, 2012 at 11:39 Comment(2)
To return nothing, obv. A function that only has side-effects and does not return a computed value is perfectly legitimate.Olshausen
In some compilers, your code can crash if it expects you to return something and you don't. The most common use is void main() in your main routine as of course, your main routine usually doesn't have to answer to anyone or anything.Serendipity
P
37

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;
    }
    
Photobathic answered 19/7, 2012 at 11:43 Comment(2)
what actually "(void)c" is doing?Parris
Explicitly discards the value of the expression on the right. It would do it anyway even if you didn't write (void), but it clarifies (to the reader and to the compiler) that you really don't care about the return value; this is used to shut down some warnings (unused returned value, unused parameter) in cases where you explicitly don't care about those values. Again, it doesn't have any particular effect on the generated code, but it's an idiom that conveys a particular meaning.Photobathic
D
8

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!

Dugout answered 19/7, 2012 at 11:45 Comment(0)
A
7

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.

Arleenarlen answered 19/7, 2012 at 11:44 Comment(3)
Actually printf returns an int value equal to the number of characters printed.Lil
@PaulR By the way, how do I do strikethrough text? I wanted to strike through the printf example so as not to invalidate your comment, but I couldn't figure it out.Arleenarlen
You can use <s>text</s> or <strike>text</strike>.Lil
S
7

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.

Schade answered 19/7, 2012 at 11:48 Comment(1)
+1 because I saw this define statement in old code, blocked-off by an ifdef with an absurd label, and couldn't figure out why anyone had written it.Whirlwind
I
3

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

Isogonic answered 19/7, 2012 at 11:42 Comment(0)
J
1

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

Jewell answered 19/7, 2012 at 11:43 Comment(5)
How about not specifying anything ? eg. just plain fooFunction(){} instead of void fooFunction(){} ?Showman
@AmitTomar In C++ you must specify a return type. In C no return type implies int.Jewell
I meant, why couldn't in C++ it be like, to tell the compiler that a function doesn't return a value, leave the return type as blank. To specify the particular type, write the type. Why a particular requirement for writing void ? Is it more of a design decision ?Showman
@AmitTomar It's specified that way. Probably because of the ambiguity that exists in the C language that C++ is based on.Jewell
@AmitTomar Also, C is (supposed to be) a superset of C; since a function without a specified type already means something in C, it must mean (close to) the same thing in C++.Whirlwind
S
1

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

Safir answered 19/7, 2012 at 11:51 Comment(0)
W
1

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.

Wallraff answered 30/8, 2014 at 20:7 Comment(0)
A
0

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

Amphimixis answered 19/7, 2012 at 11:41 Comment(1)
Huh?! The called function will still trash those registers even if it doesn't return a value.Premier
R
0

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

Radon answered 19/7, 2012 at 11:45 Comment(0)
C
0

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.

Chavira answered 26/4, 2018 at 2:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.