What does only fallback do in C ++?

I read an example about looking up gcd which is Greatest Common Divisor but only uses return in the following code. What is it? Can returns be used this way? I have searched about this and nothing seems to make me understand. Please .. Here's the code:

void fraction::lowterms ()
{
    long tnum, tden, temp, gcd;// num  = numerator and den = denumator 
    tnum = labs (num);
    tden = labs (den);
    if ( tden == 0)
    {
        exit (-1);
    }
    else if ( tnum == 0)
    {
        num = 0;
        den = 1;
        return; //why return alone    used here???
    }
}

      

+3


source to share


5 answers


In this case, nothing but the completion of the function (which would have happened anyway)

The return type of this function void

means that it does not return any value.



However, as a rule, the operator return

stops this function, returns the specified value, then no further code in this function is executed. In this case, it was at the end of the function, so it doesn't add anything.

+5


source


It ends the execution of the function and returns control to the part of the code that called the function.

return

There is no value after the keyword because the function has a return type void

and therefore there is simply no value to return.



As explained in your C ++ book!

+4


source


If it suits you, you can

 return void();

      

which is equivalent return;

, which may seem erroneous at first glance.

+1


source


return is mainly used to return to your caller function. It will not execute the below instructions in your function.

Example:

void disp()
{
    1.....
    2.....
    return
    3....
    4....
}

      

here he will not execute 3 and 4 isntructions.

0


source


Each function returns to the caller (more precisely, the command after the call) at the end, so there is always a return, even in a function with a void return (where the compiler adds return;

to the body, the programmer doesn't need to worry about that). With the manually inserted return, you have created a different exit point for your function.

0


source







All Articles