这将是在将函数分配给变量或一次又一次地调用函数之间使用函数结果的最佳方式
which will the best way to use the result of function between assigning it to a variable or calling the function again and again?
我有一个将返回值的函数,我想在多个地方使用该函数的结果。将结果存储到变量并在我想要的地方使用它,或者一次又一次地直接调用该函数,而不是将其存储在变量中并使用它。
#include<iostream>
int function(int x,int y){
return (x<<y);
}
int main(){
int a = 10;
int b = 5;
int c = function(a,b);
int d = c*c;
int e =function(a,b)*function(a,b);
return 0;
}
这完全取决于你的函数和代码。如果您的函数是
struct DeepThought {
int theAnswer() {
sleep(7.5 million years);
return 42;
}
};
那么我想将答案缓存在某个地方,以免多次支付该罚款。但是,如果您的函数比性能的最低标准快,则使用该函数的代码可能比不使用函数时更具可读性。当然,如果所讨论的函数不是纯粹的,那么这个问题就没有意义了。另请参阅记忆。
相关文章:
- 编译器如何在使用SFINAE的函数和标准函数之间确定两者是否可行
- 填充上编译器生成的复制构造函数之间的不一致
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 如何在"push_*()"和"emplace_*()"函数之间进行选择?
- 仅具有运算符()的结构和普通函数之间的实际区别
- 当两者都调用时,删除和析构函数之间的区别?
- Release() 和析构函数之间的区别?
- 调用 "project" 函数和调用 DLL 函数之间的区别
- C++模板和非模板函数之间的重载解析
- std::shared_ptr 和 std::unique_ptr 构造函数之间的不对称
- C++ 和 Lua 函数之间的交互与 3D 矢量参数
- 在结构函数之间传递文件路径 C++ 编辑:修复LNK2019错误
- 在C++同名的顶级函数之间进行选择
- 在成员函数之间传递const变量为数组的索引
- Helgrind 报告了使用 Singleton 及其构造函数之间可能存在的竞争
- 函数模板和函数之间奇怪的不一致"normal"
- 在c#和c函数之间传递值和指针
- 在C++中调用 malloc() 与"operator new"函数之间的实现差异
- 在函数之间传递相同的数组
- C++编译错误是由于使用 std::move 时运动构造函数与其他非运动构造函数之间的冲突