Search

2.2 — Void

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

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

Void is typically used in several different contexts:

1) Most commonly, as a way to indicate that a function does not return a value:

2) In C, as a way to indicate that a function does not take any parameters:

The explicit use of keyword void to mean “no parameters” is a holdover from C, and is not required in C++. The following code is equivalent, and preferred in C++:

Rule: Use an empty parameter list instead of void to indicate no function parameters are expected

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. 🙂

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

24 comments to 2.2 — Void

  • 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.

  • R4Z3R

    Hello.
    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:

  • 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

  • Vex

  • hi Alex
    I’m confused about

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

    and

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

    and

    #include <iostream.h>
    int main()
    {
        using namespace std;
    int add(int x, int y)
    {
      
      return x+y ;
    }
      
        system("PAUSE");
        return 0;//could you explain to me ‘return 0’
                                            
    }

    I have fun with your Tutorial Alex
    thanks

    • 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.

  • 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!

  • 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

  • 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

  • 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

  • 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.

  • 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.

  • Sihoo

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

  • Sibelcim, aynı sizin gibi düşünüp yapmak istediÄŸim bir yemek var.”enginarlar bit21den&#82me;:))Köfteli enginar nefis bir yemek olmuÅŸ, ellerine saÄŸlık.Etkinlik yazılarınızı da zevkle takip ediyorum.Sevgiler.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter