Side effects of functions __ function

Source: Internet
Author: User
side effects of functions

function Side effects mean that when a function is called, it also has an additional effect on the main calling function, in addition to returning the function value. For example, modify a global variable (a variable outside of a function) or modify a parameter.

The function side effect will cause unnecessary trouble to the program design, bring the procedure very difficult to find the error, and reduce the readability of the program. Strict functional language requires that functions must have no side effects.

The side effects of the function are related to several concepts, Pure function, impure function, referential transparent.

Pure Functions (Pure function)

The input-output data stream is all explicit (Explicit). Explicit (Explicit) means that there is only one channel for a function to exchange data with the outside world--parameters and return values. All input information that the function accepts from outside the function is passed through the parameter to the inside of the function. All the information that the function outputs to the outside of the function is passed through the return value to the outside of the function.

non-pure functions (impure function)

On the contrary. Implicit (implicit) means that the function exchanges data with the outside world through channels other than the parameters and return values. For example, reading/modifying global variables is called an implicit way to exchange data with the outside world.

reference Transparency (referential transparent)

The concept of reference transparency is related to and affected by the side effects of the function. If two of the same worthwhile expressions in a program can be replaced anywhere in the program without affecting the program's actions, then the program has referential transparency . The advantage is that it is easier to understand and less obscure than the semantics of a language that is not referenced transparently. Pure functional languages have no variables, so they all have referential transparency.

The following example illustrates the combination of reference transparency and function side effects?

1 2 3 RESULT1 = (Fun (a) + B)/(Fun (a)-C); temp = Fun (a); RESULT2 = (temp + b)/(TEMP-C);

If the function has no side effects, then RESULT1 and RESULT2 will be equivalent. However, if fun has side effects, such as having B or C plus 1, then RESULT1 and result2 will not be equal. As a result, side effects violate referential transparency.

In JavaScript, a function is introduced. But obviously the functions in JS can access, modify global variables (or variables defined outside the function), as follows?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.