-
void?
i was reading a tutorial and it was about classes. They declare sum things, well ill paste it
Code:
class Dog {
public:
void setAge(int age);
int getAge();
void setWeight(int weight);
int getWeight();
void speak();
private:
int age;
int weight;
};
well wat does the void before setage and setweight mean?
-
It means the function is not returning a value.
-
so wat r u sayin, i can set sumthin but i cant output it on da screen?
-
No what he is saying is that the function does not return a value eg
Code:
int foo()
{
return 1;
}
void bar()
{
cout<<"Hello";
}
-
i know, im a dumbass! but plz explain better
-
OK when you have a function it has to be a type int,float,char,or void or some other value. void does not return a value. You can think of it like this.
Code:
#include <iostream>
int add(int a,int b);
float divide(int a,int b);
void printStuff(int a,int b);
int main(void)
{
int addResults = add(10,5);//This is setting the variable addResults to the return of the function add()
float divideResults = divide(9,4);//doing the same as the first but with divide
std::cout<<addResults<<std::endl;
std::cout<<divideResults<<std::endl;
printStuff(10,10);//this will do whats in the funtion
std::cin.get();
return 0;
}
int add(int a,int b)
{
return (a+b);
}
float divide(int a,int b)
{
return (a/b);
}
void printStuff(int a,int b)
{
std::cout<<"a equals: "<<a<<std::endl;
std::cout<<"b equald: "<<b<<std::endl;
//notice it doesn't return a value!
}
-
u guys a really confusing sumtimes
-
What are you confused about? i don't know any other way of explaining it to you if you can't get such a simple concept maybe look into a different thing to pass your time
-
just explain wat u mean by returning a value
-
Just as it sounds a function RETURNS A VALUE
Code:
int blah()
{
return 1;//THIS FUNCTION RETURNS 1!!!!!!!!!!!
}
float meh()
{
return 4.5//THIS FUNCTION RETURN 4.5!!!!!!!!!!!
}
int main(void)
{
weee=blah();//THIS EQUALS 1 BECAUSE blah() RETURNS 1!!!!!!!!!!!!!!!
yeppers=meh();//THIS EQUALS 4.5 BECAUSE meh() RETURNS 4.5!!!!!!!!!!!!
return 0;//MAIN RETURNS 0!!!!!!!!
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! just for some extra effect
-
thankyou, was that so hard?
-
No, but is understanding what return means so hard?
-
it is wen crappy tutorials dont explain , i still dont get it but i aint askin no more, but dont make me come over there biatch(kiddin)
-
Hi polonyman,
I'm also really new to C++ (and new to the boards here). I found functions a little bit tricky at first too. As a test of how well I understand them let me try and give you my two cents. If I'm wrong, then I'll be glad to hear so from the more experienced folk here.
I think of functions as kinda like a really big variable, but a variable that does something. You can put stuff in it (arguments) and get stuff out of it (return) if you choose to . You have to tell it, though, what kind of arguments you want it to accept as well as what kind of value to spit back out (return). It spits back the return value at the point where you called the function (usually, I guess, in main, but I suppose it could be called from another function just as easily?).
The void in front of the function means that you don't want it to return any value.
You can still output to the screen from a function (for ex. with cout) but this is different than returning a value.
Does this sound right?
It actually makes me think just a bit about the return value of main. If functions return their values to main (or some other place within the program) where does main return its value to? My first inclination would be that it returns either to the compiler or to the OS. But I can't be sure of that.
Anyway, don't know if this helps at all, but hope you can get it sorted out.
-
it (one function) could be called from another function just as easily?-----Yes, one function can be call from another and return to calling.
it (main()) returns either to the compiler or to the OS---main() returns a value to the OS. What the OS does with the return value is up to the OS.