How does the return command work?

Thanks in advance for any help you guys can give me! =D

Printable View

- 07-06-2005Raelieanreturn command
How does the return command work?

Thanks in advance for any help you guys can give me! =D - 07-06-2005xErath
it ends the execution of the function where it is inserted, evaluates the expression which holds, and sends it back to the caller

Code:`int foo(){`

return 4;

}

void bar(){

int val;

val = foo();

// val has value 4

}

- 07-06-2005C+noob
also putting return 0; ast the end of the program means the program has ran successfullygenerally if you wanted to make a multiplication function

Code:`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

}

- 07-06-2005C+noob
i did it real fast so there might be minor errors. hope it helps

- 07-06-2005dra
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