Home > Mobile >  How to use the return value from one function in another in C
How to use the return value from one function in another in C

Time:12-21

Is it possible to use the return value from one function in another?

The code is something like this:

double process () {
       
     //doing something

       return result;
}

double calculation () {

       double sum = 0;
       sum = result   10; //Want to use the result from the previous function here
   
       return sum;
}

Thanks!

CodePudding user response:

You have two options - you could store the return value in a variable and then use that variable, or you can use the function call directly.

Store in a variable like this -

double sum = 0, result = process();
sum = result   10;

or use the call directly like this -

double sum = 0;
sum = process()   10;

CodePudding user response:

We can directly call the function:

double calculation () {

       double sum = 0;

       sum = process(//something)   10; 

       return sum;
}

CodePudding user response:

In case you don't want to call process inside calculation then you can do something like the below:

double process() {

    // doing something

    return result;
}

double calculation ( const double result ) {

    double sum = 0;
    sum = result   10;

    return sum;
}


int main( )
{
    double sum = calculation ( process( ) ); // pass the output of process to
                                             // calculation as an argument
}
  •  Tags:  
  • c
  • Related