D.2.2 — Void



Void is typically used in several different contexts:





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

D.2.3 -- Variable sizes and the sizeof operator
D.2.1 -- Fundamental variable definition, initialization, and assignment

37 comments to D.2.2 — Void

  • 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
