Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return to function instead of "0" or "variable "

Tags:

c

This is a theoretical question. To understand further how a return statement at the end of a function can be manipulated.

Can we do like this:

int intialize1()
{
    ...do smth;
    ...do smth;

    return initialize2();
}

in other c file

int initialize2()
{
   ...do other thing;
   ...do other thing;

   return 0;
}

Is this a proper way to do like this?

like image 506
danteDev Avatar asked Apr 09 '26 00:04

danteDev


2 Answers

What you wrote is completely correct, frequently done, and functionally equivalent to:

int initialize1()
{
    ...do smth;
    ...do smth;

    int x_value;              // Create a place to store the return value
    x_value = initialize2();  // Get the result of the function, and store it.
    return x_value;           // Return the stored value.
}
like image 146
abelenky Avatar answered Apr 11 '26 13:04

abelenky


it is perfectly legal to call function from return statement, but you have to make sure that return types of both functions are consistent and there are no data truncation or semantically wrong implicit conversion

like image 38
dolphinden Avatar answered Apr 11 '26 13:04

dolphinden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!