简体   繁体   English

副作用和参考透明度有什么区别?

[英]What is the difference between side effect and referential transparency?

What is the difference between these terms? 这些条款有什么区别?

  • a function has no side effect 一个函数没有副作用

  • a function is referential transparent 一个函数是引用透明的

A function that is referentially transparent definitely has no side effects. 一个引用透明的功能肯定没有副作用。 However, a function that has no side effects is not always referentially transparent. 但是,没有副作用的功能并不总是在引用上透明。

Examples: 例子:

int x = 0;

int referentiallyTransparent(int y)
{
    return y + 1;
}

int hasNoSideEffects(int y)
{
    return x + y;
}

The function hasNoSideEffects has no side effects. 函数hasNoSideEffects没有副作用。 Which means that calling hasNoSideEffects(y) does not affect the state of the program. 这意味着调用hasNoSideEffects(y)不会影响程序的状态。 However, the value it returns is affected by the state of the program and does not solely depend on the input variable y and thus a call hasNoSideEffects(y) cannot always be replaced by the value it reduces to. 但是,它返回的值受程序状态的影响,并且不仅仅取决于输入变量y ,因此调用hasNoSideEffects(y)不能总是被它减少的值替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM