How does the return command work?
Thanks in advance for any help you guys can give me! =D
Printable View
How does the return command work?
Thanks in advance for any help you guys can give me! =D
it ends the execution of the function where it is inserted, evaluates the expression which holds, and sends it back to the caller
calling function foo will return 4 to its callerCode:int foo(){
return 4;
}
void bar(){
int val;
val = foo();
// val has value 4
}
also putting return 0; ast the end of the program means the program has ran successfullygenerally if you wanted to make a multiplication function
i think thats good enough :PCode:int a;
int b;
int c;
mult( a, b ) {
a * b = c;
return c;
}
// this would multiply the vars passed iunto it and return the value of c
// ex.
int main()
{
cout<<"enter the first number to be multiplied\n";
cin>> a; // put the uisers input into a
cout<<"\n the second? \n";
cin>> b; //put the users value into b
cout<<"\n the calue of << a <<" x "<< b << is << mult( a, b )"\n";
// this will return 4 if both numbers entered are 2
}
i did it real fast so there might be minor errors. hope it helps
Also, you should note that you need to return the same type as the function.
Code:int main()
{
return 0;
} //0 is a valid integer
float function()
{
return 3.14;
}//3.14 is a valid float value
istream& input( istream& input )
{
return input;
}//input is an input stream