void function

Feb 24, 2009 at 6:59pm
void is the type of function which doesn't need to return
then why is this program not working ?

#include <iostream>
using namespace std;
void addition(int a,int b);
{
int r;
r=a+b;
}
int main()
{
int z;
z=addition(5,4);
cout <<"the result is "<<z;
return 0;
}
Feb 24, 2009 at 7:04pm
Void doesn't return any value: here you are trying to store the returnvalue of the function into z, but the function doesn't have a returnvalue, and so it's not valid. addition() should be of type integer and return r.
Feb 24, 2009 at 7:14pm
can you please correct this program , void should be in there
Last edited on Feb 24, 2009 at 7:15pm
Feb 24, 2009 at 7:15pm
No, do it yourself.
Feb 24, 2009 at 7:33pm
firedraco is right, you need to do it yourself. But a little hint: you'll need pointers.
Feb 24, 2009 at 7:43pm
And watch out for "extra" semicolons.
Feb 25, 2009 at 12:50am
I would suggest using an integer return type because that function doesn't do a thing. The main function can not access the variable r.
Feb 25, 2009 at 7:19am
The OP said he should use type void: this is probably an assigment to practize with pointers.
Feb 25, 2009 at 12:42pm
Globals work too.
Topic archived. No new replies allowed.