4.2 — Void

Void is the easiest of the data types to explain. Basically, void means “no type”!

Consequentially, variables can not be defined with a type of void:

Void is typically used in several different contexts.

Functions that do not return a value

Most commonly, void is used to indicate that a function does not return a value:

If you use a return statement to try to return a value in such a function, a compile error will result:

On Visual Studio 2017, this produced the error:

error C2562: 'noReturn': 'void' function returning a value

Deprecated: Functions that do not take parameters

In C, void is used as a way to indicate that a function does not take any parameters:

Although this will compile in C++ (for backwards compatibility reasons), this use of keyword void is considered deprecated in C++. The following code is equivalent, and preferred in C++:

Best practice

Use an empty parameter list instead of void to indicate that a function has no parameters.

Other uses of void

The void keyword has a third (more advanced) use in C++ that we cover in section 10.20 -- Void pointers. Since we haven’t covered what a pointer is yet, you don’t need to worry about this case for now.

Let’s move on!

4.3 -- Object sizes and the sizeof operator
4.1 -- Introduction to fundamental data types

47 comments to 4.2 — Void

  • Mal

    Why in the examples is the int variable not being list initialized?

  • Anon

    It's 2020 and I'm reading this. Just an appreciation comment. Great job !!!

  • Wallace

    Minor typo: "Most commonly, void *as* a way to indicate that a function does not return a value" seemingly should be "Most commonly, void *is* a way to indicate that a function does not return a value."

    Thanks for the great site!

  • Edwin Martens

    as far as I know, there IS a variable that can be of the type void... a Pointer !
    void * pPointer;

  • Is this considered valid in C++?

    (I've done this a lot in other languages, and that would have a 'return' statement inside a void). :-P

  • Fateh Chadha

    Hi Alex..!!
    void writeValue(int x) // void here means no return value
        std::cout << "The value of x is: " << X << std::endl;
        // no return statement, because the return type is void

    In this example above... since void has no return, where do we get the value of capital X from.
    While making a calculator, in void printValue() function.. only thing I couldn't understand was how this x is interpreted in this function. If u could clear this doubt it would be a great help.

    • Hi Fateh!

      There is no capital x in this lesson. Lower case @x is a parameter of @writeValue (Lesson 1.4a).

    • Qais

      Hello , I think you should go back to chapter one and learn back the parameters.Its always bad idea to skip because programming is complicated.

      The answer to your question would be that. X is parameter. Parameters take his value from caller . That means whichever functions call the void writevalue(int x).  It will initializate the value to X.

      void writeValue(int x)
          std::cout << "The value of x is: " x << std::endl;
      int main()
         writeValue(5);  // The caller is assigning value to the function.  Thats why we use parameters.
         return 0;

  • Aditi

    Hello ! Just a small doubt, if the words like void , int , are used to specify data types of variables, why do we also use them before names of functions like int main (), void main(), etc.? Does the function int main () mean that the function will only accept integer values and return integer values ?

    As a side note , thanks a ton for for this amazing website ! This is my only source of learning C++

  • Linyuan

    can't imagine that this site was already there in 2007!

    know this in's just toooooooooooooo late!!!

    if i met this website much more earlier all things would be totally different...

    thanks again!

  • Sihoo

    The only site I am more than happy to disable AdBlock.

  • can someone check this code for me.

    I am not able to compile and run it, if someone can compile and run it and reply me with the output I will be very thankful.

    Thanks in advance.
    Best Regards,
    Tanay Jaiman

    • Lyle

      Lines 13, 23 just print the letter "n", is that what you intended ?

      Line 20 needs a terminating ";" at the end.

      Your "operation = "  set each need to use "==" to do comparisons.

      I commented out the "goto" because you never setup a "label:" for it to work. You can't arbitrarily pick a place for code to go-to, it's more involved than that (if you even should be using a goto, which is unlikely).

      Hope that helps you along.

    • McSteven

      And you would use single quotes for checking if the operator is legal. Consequently, you would declare the variable 'operator' with a char type.

  • Georges Theodosiou

    Mr Alex good morning (I live in France),

    Please let me express my many thanks for your reply.

    With regards and friendship

    P.S. I have problem connecting your site.

  • Georges Theodosiou

    Mr Alex, good morning,

    Please, let me ask you the complete program for:

    int getValue() // empty function parameters is an implicit void
        int x;
        std::cin >> x;
        return x;

    With regards and friendship

    • Alex

  • Michael

    Hi I want to ask you something about void. I'm practicing making program that adds 2 to any number the user inputs and it works fine. I separate it to two files. One is for the main and one is for the functions. It all works fine but I read in this article that you can't declare a void, right? Because void means no type. But I forward declare a void and nothing's wrong the program runs. I'm just curious tho

  • Milo

    Hi Alex
    Please can you advise
    I'm dealing with a C++ question and it states that the function should calculate the sum of two parameters passed by value and then store the result in the first variable passed by reference. It should calculate the difference between the two parameters passed by value and then store the result the second paramete passed by reference

  • Zigzem30

    Hi, I was making a program where it finds the circumference of a circle. I looked online but I can't find out how to use augments to do a function call to circumference in void. Thanks for any help!

  • hi Alex
    I'm confused about

    int add(int x, int y)
      return x+y;


    void add(int x,int y)
    return x+y;
    in this case the keyword void doesn't return any value or 0


    #include <iostream.h>
    int main()
        using namespace std;
    int add(int x, int y)
      return x+y ;
        return 0;//could you explain to me 'return 0'

    I have fun with your Tutorial Alex

    • Alex

      A function with a void return value will not return anything to the caller.

      For example:

      But this would be okay:

      Function main() returns a value to the operating system indicating whether the program ran succesfully or not. A return value of 0 means success. A return value otherwise indicates a failure.

  • Vex

  • Ian

    I am still confused it is so weird like you can't declare an variable and I heard about that they can write functions like iostream

  • R4Z3R

    Why in the function `main` we use void?

    what is the different between `main(void)` and `main()` ?

    • Alex

      The following two function declarations are identical in C++:

      • Jeroen

        In practice, you may not do that with the main function.
        Windows or what o.s. than also expected a value back.

        For non-pointer success/fail:

        For pointer functions:

  • Does C++ retain the function property in C- "if no return type is declared, it will be assumed to be 'int' by the compiler"?

    • deltron zero

      As far as I can tell (I barely started learning here), it does so with main(), but you'll get an error when you compile if you don't declare a return type for any other function.

    • Alex

      No. The C++ standard says that a function missing a return type is ill-formed.

      That said, some compilers that do not strictly adhere to the C++ standard will still assume integer, presumably for compatibility with C. It looks like Visual Studio allows you to omit the return type for main(), but not other functions.

      That said, you should always declare a return type for functions in C++, even if your compiler allows you to do otherwise.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]