![](/img/trans.png)
[英]How to return a variable from one function in C++ to main then use it in another function?
[英]How to use the return value from one function in another in C++
是否可以在另一個 function 中使用返回值?
代碼是這樣的:
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;
}
謝謝!
您有兩個選擇 - 您可以將返回值存儲在一個變量中,然后使用該變量,或者您可以直接使用 function 調用。
存儲在這樣的變量中 -
double sum = 0, result = process();
sum = result + 10;
或像這樣直接使用調用 -
double sum = 0;
sum = process() + 10;
我們可以直接調用function:
double calculation () {
double sum = 0;
sum = process(//something) + 10;
return sum;
}
如果您不想在calculation
中調用process
,則可以執行以下操作:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.