Void vs INT function

<p> I just started learning C++ and im very confuse with this functions </p>
<br>Why do i need to use void when it does not return a value?
Last edited on
Why do i need to use void when it does not return a value?
To indicate that it should not return anything

Also:
1
2
void foo(); //Declaration of function without arguments and returning nothing
     foo(); //Call to function foo taking no arguments 
Why do i need to use void when it does not return a value?

Well, let's say you have a function declared like this:
 
   int f1();
we can see this function will return a value of type int.

What if we wanted a function which did not return any value at all? Well, you might think it could be declared like this:
 
f2();
in other words simply omit the return type.

But - there are at least two problems with that. One, that declaration looks exactly the same as when we call the function, so it is not clear what is the intended meaning. Secondly, historically if the return type was omitted (in the C language), the compiler automatically assumed that the function would return an int.

So instead we use the declaration
 
void f2();
this makes it clear that it is a declaration rather than a function call, and we know for certain that the function will not return a value. Think of the word 'void' here as meaning 'nothing at all, not even empty space'.


Hello Miinipaa
i dont understand you but example if write int instead of void it does not affect the program
write int instead of void it does not affect the program
It does.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void foo()
{
    return; //Empty return statement, legal
}

void foo()
{
    //No return at all, legal
}

int foo()
{
    return; //Empty return statement, compiler error
}

int foo()
{
    //no return, illegal, can result in stack corruption
}
Hello chervil
I understand it but can you make it in a simpler way
Miniipaa
I see I see its clearer now :D but when i write

1
2
3
4
void foo()
{   
    return;
}
it says undefined reference to winmain@16
You probably get that error message because you don't have a main function in your program.
You either have done something wrong or your compiler is faulty:
http://coliru.stacked-crooked.com/a/2d354807605b36ec
http://ideone.com/0q9M4N
Im using code::blocks 10.01 mingw


Hello peter
i see so i cannot write this without main function
1
2
3
4
void foo()
{   
    return;
}
You could, and it should compile, but if you try to build (link) the program to create an executable file it will complain that a main function is necessary.
by the way were out of the topic thanks again Miniipaa,Chervil,Peter
Peter is right, your program doesn't have a main function, so your compiler then tried to look for a WinMain function, which you also don't have (don't worry about that).
You call your foo() function from your main function. Your main function is implicitly called when the program runs, it's where everything starts.

1
2
3
4
5
6
7
8
9
10
void foo()
{
    std::cout << "Test" << std::endl;
    return;
}
int main()
{
    foo();
    return 0;
}
Last edited on
Topic archived. No new replies allowed.