O
O
Old Odessa2015-10-12 17:31:20
Programming
Old Odessa, 2015-10-12 17:31:20

Can't understand what is VOID in C++?

I don't really understand what it's about. It is possible on examples for the beginner that it would be more clear.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
Z
Zelimkhan Beltoev, 2015-10-12
@vs_convoy

You can think voidof it as an empty or undefined value.
For example, it can be used:

  • in functions: as return value:
    void print_hello()
      {
          printf("Hello!");
      }

    Note that print_hello()you can also write this: print_hello(void)
    That is, we can explicitly show that the function does not accept anything (usually voidomitted when writing)
  • as a pointer to an undefined data type:
    void *value; // указатель на что-то неопределенное
    int a = 5;
    value = (int *)a; // теперь указываем на целое

    To put it simply, I wouldn't focus on it. Just remember: if the function does not return anything , just write at the beginning void, otherwise - the return data type. And that's it: you shouldn't bother yourself with this anymore

M
Maxim Grechushnikov, 2015-10-12
@maxyc_webber

do you see the bagel? you perform a function and eat it. what do you have left of the donut? true? false? null? void!

E
Evgeny Ivanovich, 2015-10-12
@makrushin-evgenii

void = procedure

I
Ilya Bobkov, 2015-10-12
@heksen

procedure

T
tsarevfs, 2015-10-12
@tsarevfs

void is specified as the return type of a function if it returns nothing. For example, the int get_speed() function, which returns the unit's speed as an integer, is defined with type int. But void set_speed(int speed) does not return anything, but rather sets the speed. That's why we use void.

D
Dmitry, 2015-10-12
@EvilsInterrupt

Vlad Convoy It's simple. From the course Computer science. I recommend reading SICP!!!
In mathematics To the question "What does it look like?" replies "function". And to the question "How to get it?" The answer is "procedure".
In C-like languages, named computations are written as functions. But functions in C-like languages ​​answer both questions, and there is no way to tell the difference. A function in C-likes must always return something! Some result! Some new object. But something new must be returned anyway!
Now, what if you want to at least somehow distinguish between functions and procedures? Here for this there is a "crutch" in the form of 'void'! In other words, it's a way of saying to the reader of the code, "we're not returning anything, we're only creating." This is "how to get it?" in such functions only the production of something new. Draw a window, encrypt a disk, send it to a friend .... And in the functions that return the result, you can already write in terms of mathematics, that is, what it looks like, but not how it turns out;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question