无需发送任何内容即可返回函数(Get return of a function without sending anything)

我有一个函数头:

double countThis(double counter);

然后,在我的主要内容中我这样做:

double test = 10; countThis(test);

然后是功能:

double countThis(double counter) { double counted = counter; return counted; }

在底部,我有一个最后一个函数,在这里我想得到double counted而不必做countThis(something) ,我只想从上一次调用返回在main中获得并获得值10(计数) )。

I have a function header:

double countThis(double counter);

Then, in my main I do this:

double test = 10; countThis(test);

Then comes the function:

double countThis(double counter) { double counted = counter; return counted; }

On the bottom, I have one last function, and here I want to get double counted without having to do countThis(something), I just want to get the return from the previous call that was made in main and get the value 10 (counted).

最满意答案

实现此类持久性的一种方法是使用类并定义该类的实例

struct Counter { double counted; double countThis(double counter) { return counted = counter; // assign counter to counted, and return that value. } };

在使用时:

int main() { Counter c; c.countThis(10); // c.counted contains the last value sent to countThis, in this case, 10 }

实例 c用于保存您传递给countThis的值。

One way to achieve this sort of persistence is to use a class and define an instance of that class:

struct Counter { double counted; double countThis(double counter) { return counted = counter; // assign counter to counted, and return that value. } };

At the point of use:

int main() { Counter c; c.countThis(10); // c.counted contains the last value sent to countThis, in this case, 10 }

The instance c is used to persist the value that you pass to countThis.

无需发送任何内容即可返回函数(Get return of a function without sending anything)

我有一个函数头:

double countThis(double counter);

然后,在我的主要内容中我这样做:

double test = 10; countThis(test);

然后是功能:

double countThis(double counter) { double counted = counter; return counted; }

在底部,我有一个最后一个函数,在这里我想得到double counted而不必做countThis(something) ,我只想从上一次调用返回在main中获得并获得值10(计数) )。

I have a function header:

double countThis(double counter);

Then, in my main I do this:

double test = 10; countThis(test);

Then comes the function:

double countThis(double counter) { double counted = counter; return counted; }

On the bottom, I have one last function, and here I want to get double counted without having to do countThis(something), I just want to get the return from the previous call that was made in main and get the value 10 (counted).

最满意答案

实现此类持久性的一种方法是使用类并定义该类的实例

struct Counter { double counted; double countThis(double counter) { return counted = counter; // assign counter to counted, and return that value. } };

在使用时:

int main() { Counter c; c.countThis(10); // c.counted contains the last value sent to countThis, in this case, 10 }

实例 c用于保存您传递给countThis的值。

One way to achieve this sort of persistence is to use a class and define an instance of that class:

struct Counter { double counted; double countThis(double counter) { return counted = counter; // assign counter to counted, and return that value. } };

At the point of use:

int main() { Counter c; c.countThis(10); // c.counted contains the last value sent to countThis, in this case, 10 }

The instance c is used to persist the value that you pass to countThis.