Kabir
Kabir

Reputation: 43

Return values in C

void draw( int I, long L );
long sq( int s );

int main()
{
    long y;
    int x;

    y = sq( x );
    draw( x, y );
    return();
}  

long sq( int s )
{
    return( s * s );
}

void draw( int I, long L )
{
    return;
}

What is the difference between return(), return (S*S) and return? Please give an explanation.

Upvotes: 0

Views: 270

Answers (2)

d_inevitable
d_inevitable

Reputation: 4451

There is hardly any difference.

Basically there are two syntaxes.

First of all:

return somethinghere;

is exactly the same as

return (somethinghere);

You can replace "somethinghere" with anything you want (as long as it qualifies with the function's return type), an equation another funciton anything that has a value, including simply nothing if the return type is "void".

If you put nothing, then it means you function returns nothing, otherwise you are returning the result of whatever you have placed there.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258558

Well:

return(); is illegal, have you tried compiling?

return(s*s) is the same as return s*s; and it tells the function what value to return.

For example, if you would have:

 long x = sq(1);
 //x would be 1 here

return; exits from a void function. You can't put an empty return statement inside a function with a non-void return type. Put at the end of a void function, it does nothing. But you can use it to exit the function early:

void foo()
{
    if ( someCondition )
       return;
    statement1;
    statement2;
    return;
}

The first return has the effect that it will exit the function if someCondition is true. So the statements will not be executed. The second return makes no difference whatsoever.

Upvotes: 5

Related Questions