Reputation: 123
I am new to C++ and I am contributing to a rather large project. I have written a piece of code and I am calling an external function that carries out a bunch of calculations.
I need external_function()
to run through completely, but I also need the value of a specific variable (a double) that is calculated during external_function()
. I would like to store or at least use the value of this variable in my_function()
. Is this possible? Something along the lines of
double my_variable = external_function(external_variable);
Note the code looks like this:
void external_function()
{
double d;
// perform calculations on d
}
void my_function()
{
...
external_function();
...
}
Unfortunately, external_function
does not return anything, it just does calculations and prints some output. As the entire code of the project is already rather complex, I would like to change as little as possible in the part of the code that has not been written by me. I would really appreciate your help!
Upvotes: 2
Views: 786
Reputation: 7111
I'm assuming here you have code like the following:
void external_function()
{
double d;
// perform calculations on d
...
// print d
std::cout << d;
}
void my_function()
{
...
external_function();
...
}
I'll assume external_function
takes no parameters, but it really doesn't matter if it does.
You can change external_function
by modifying the return type to double
:
double external_function()
{
double d;
// perform calculations on d
...
// print d
std::cout << d;
return d;
}
The function can still be called safely like so:
external_function();
Without catching the return value, so there is no need to update other uses of it. Some static code analysers might hassle you about ignoring the return value of a function, but you can probably write an exception for them if you want.
This now means you can call it like so:
double value = external_function();
Your second option would be to pass an optional parameter to external_function()
:
void external_function(double* out = nullptr)
{
double d;
// perform calculations on d
...
// print d
std::cout << d;
if (out)
*out = d;
}
The same thing goes: callers can still call this function without changing the way they call it:
external_function();
But it means you can now call it like this:
double value = 0.0;
external_function(&value);
// value now contains the same value as d
Upvotes: 2
Reputation: 447
You can use a class member variable of double type. Assign the calculated value of "d" to the member variable.
Now you can use this member variable in any method of your class.
or You can pass a reference parameter from your calling method and assign the value of "d" in the externalfunction. Eg:
externalFunction(double &updatedValue)
{
//do calculation of d
updatedValue =d;
}
void my_function()
{
double value;
externalFuntcion(value);
//now value will have the value of d;
}
Upvotes: 1
Reputation: 772
Mode 1 : Use void external_function()
, adding parameter.
void external_function(double* pValue)
{
*pValue = external_value;
// operation of external_value
// external_value = 142.14
}
Get result
void call_function()
{
double pValue = 0.0;
external_function(&pValue);
cout<<pValue<<endl;
}
Result : 142.14
Mode 2 : Use no parameter function, modifying return type of function.
double external_function()
{
// do something you want...
// external_value = 142.14;
return external_value;
}
Get result
void call_function()
{
double pValue;
pValue = external_function();
cout<<pValue<<endl;
}
Result : 142.14
Upvotes: 0
Reputation: 437
Yes, it's possible to store the value of external_function(external_variable)
in a variable.
Be sure to check that the return type of external_function
is double
and, therefore, returns a double value. You will need to code it like this :
double external_function() {
double returnedValue;
// your code here
cout << calculationOutputValue << endl;
return returnedValue;
}
Upvotes: 1
Reputation: 21510
If the function external_function
returns a double, then yes, you can store that in a double as you have shown in the question. That will be perfectly fine.
If the double
variable you are talking about is a local variable in that function that is not returned or stored in a variable passed to the function by reference, then you do not have any way of fetching that.
Upvotes: 1