维基百科是这样说的:
In computer science, an operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside its local environment, that is to say has an observable effect besides returning a value (the main effect) to the invoker of the operation.
但是我们如何访问其局部环境之外的变量,谁能解释清楚这种情况、副作用、主要作用和序列点?
最佳答案
函数是(应该是)一个黑盒子,其中返回值或通过引用传递的变量的值应该是唯一可能根据输入参数而改变的东西。
函数在这些情况之外产生的任何其他可观察到的变化都是副作用。最著名的例子可能是 printf()
函数,它除了返回写入的字符数外,还更改标准输出的内容,这意味着更改与管道关联的一些内存缓冲区,a例如,文件或屏幕,并且不属于函数的本地环境。
关于c - C中的副作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62148339/