what is the function of return ();

Dear fellows I tested the following example with return (r); n without return (r); the answer is same..then why return (r); has been used here???

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
Last edited on
If you remove the return (r);, that code shouldn't compile.

Returning a value specifies the "output" of the function. When you assign a function call to a variable, you are actually assigning whatever that function returns.

z = addition(5,3);

Here, addition will be called with a=5 and b=3.
In addition, r is set to 5+3 (8)
You then return r.

Since r is being returned from addition, that is the value that gets assigned to z in the above line.
Topic archived. No new replies allowed.