Void vs INT function

Jun 10, 2014 at 11:43am
<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 Jun 10, 2014 at 11:44am
Jun 10, 2014 at 11:50am
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 
Jun 10, 2014 at 11:55am
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'.


Jun 10, 2014 at 11:58am
Hello Miinipaa
i dont understand you but example if write int instead of void it does not affect the program
Jun 10, 2014 at 12:01pm
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
}
Jun 10, 2014 at 12:05pm
Hello chervil
I understand it but can you make it in a simpler way
Jun 10, 2014 at 12:09pm
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
Jun 10, 2014 at 12:11pm
You probably get that error message because you don't have a main function in your program.
Jun 10, 2014 at 12:14pm
You either have done something wrong or your compiler is faulty:
http://coliru.stacked-crooked.com/a/2d354807605b36ec
http://ideone.com/0q9M4N
Jun 10, 2014 at 12:19pm
Im using code::blocks 10.01 mingw


Jun 10, 2014 at 12:22pm
Hello peter
i see so i cannot write this without main function
1
2
3
4
void foo()
{   
    return;
}
Jun 10, 2014 at 12:25pm
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.
Jun 10, 2014 at 12:25pm
by the way were out of the topic thanks again Miniipaa,Chervil,Peter
Jun 10, 2014 at 12:25pm
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 Jun 10, 2014 at 12:26pm
Topic archived. No new replies allowed.