简体   繁体   中英

Return a value from a function

I wanted to ask a newbie question about returning a value from a function here's my simple program :

#include <stdio.h>
double calcule(double r1,double r2, double r3)
{
    double serie;
    double parallele;

    serie = r1 + r2 + r3;
    parallele = (r1*r2*r3)/(r1*r2+r1*r3+r2*r3);
    double results[2] = {serie,parallele};
    return *results;
}
int main()
{
    double r1;
    double r2;
    double r3;
    printf("Veuillez entrez les valeurs R1,R2 et R3");
    scanf("%lf",&r1);
    scanf("%lf",&r2);
    scanf("%lf",&r3);
    double res[1] = {calcule(r1,r2,r3)};
    printf("Si les resistances sont branchées en série la resistance equivalente est : %f",res[0]);
    printf("Si les resistances sont branchés en parallele la resistance equivalente est : %f",res[1]);
    return 0;
}

My question is why I have to do "return *results;" instead of just doing "return results;" ?

I see multiple problems in your code.

a) The scope of the array variable 'results' that is declared in the function calcule is limited to the function itself. Once the program finishes executing the block of statements (function) the variable location pointed by results is no longer valid. Trying to access the location pointed by results post its scope can sometimes lead to access violation faults.

b) you are declaring variables at location that please you. Your code compiles well with C99 std but suffers when compiled with c89 and c90. Its best to do all the type declaration statements at the beginning of the block for portability.

#include "stdio.h" 
double calcule(double r1,double r2, double r3) {
    double serie;
    double parallele;    
    serie = r1 + r2 + r3;
    parallele = (r1*r2*r3)/(r1*r2+r1*r3+r2*r3);
    double results[2] = {serie,parallele};     /* a) scope is restricted to this block */
    return *results; 
}

int main() {
    double r1;
    double r2;
    double r3;
    printf("Veuillez entrez les valeurs R1,R2 et R3");
    scanf("%lf",&r1);
    scanf("%lf",&r2);
    scanf("%lf",&r3);
    double res[1] = {calcule(r1,r2,r3)}; /* b) type declaration not allowed here in c89/c90 */
    printf("Si les resistances sont branchées en série la resistance equivalente est : %f",res[0]);
    printf("Si les resistances sont branchés en parallele la resistance equivalente est : %f",res[1]);
    return 0; 
}

Instead of me writing the code, i will give you the function prototypes. You can use them to build your functions.

void calcule(double r1, double r2, double r3, double *output);
int main (void){
    ...
    double output[2];
    calcule(r1,r2,r3,output);
    ...
}

*result is == return[0] so:

return *results;

is:

return results[0];

that is equivalent to:

return serie;

serie is 0th element in result array.

I don't know much about your code but what useful your calcule() function is doing is just equivalent to

double calcule(double r1,double r2, double r3)
{
    return r1 + r2 + r3; // because serie = r1 + r2 + r3;
}

because all other variables are local to function.

Note: Although return type of your function is double if you return result it would be an error as result is an array of double (but not a double). Additionally, by returning result you will return address of local object that causes Undefined behavior in your code.

Because return type of your function is double . *results is of type double while results is of type pointer to double,ie, double * , so you can't return a pointer from a function whose return type is defined as double .

The function returns a single value of type double . The value results is an array of two double values. Returning *results is equivalent to returning results[0] , the first element in the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM