Function side effects

Source: Internet
Author: User

Function side effectsWhen a function is called, in addition to the returned function value, it also has an additional impact on the main function call. For example Modify global variables (variables outside the function) or Modify parameters.

 

function side effects bring unnecessary trouble to the Program Design and very difficult to find errors to programs, and reduce the readability of the program. strict functional languages are required. Evaluate the function without any side effects.

 

Several concepts related to function side effects,
Pure function,
Impure function,

Referential transparent.

 

pure function

the input and output data streams are all explicit. explicit (explicit) means that functions exchange data with the outside world only through one channel-parameters and returned values. All input information accepted by the function from outside the function is passed to the function through parameters. All information output from the function to the external function is passed to the external function through the return value.

non-pure functions (

Impure Function

)

.

implicit (implicit) means that a function exchanges data with the outside world through parameters and return values. For example, reading/modifying global variables is called implicit data exchange with the outside world.  

 

reference transparency (

referential transparent )

the reference transparency concept is related to the side effects of the function and is affected by it. If two identical expressions in the program can replace each other anywhere in the program without affecting the action of the program, then the program has reference transparency . Its advantage is that it is easier to understand and not so obscure than non-reference transparent language semantics. Pure Functional Languages have no variables, so they all have reference transparency.

 

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

 
Result1 = (fun (A) + B)/(fun (a)-C); temp = fun (a); result2 = (temp + B) /(temp-C );

If the function has no side effects, result1 and result2 are equivalent. However, if fun has side effects, such as adding B or C to 1, result1 and result2 are not equal. Therefore, the side effects violate the transparency of reference.

Functions are introduced in JavaScript. But obviously, functions in JS can access and modify global variables (or variables defined outside the function), as follows:

VaR A = 5; function fun () {A = 10;} fun (); // A is changed to 10

To ensure that functions have no side effects in Javascript, you can only rely on the habits of programmers, that is

1. Function entry uses parameter operations without modifying it

2. The function does not modify variables outside the function, such as global variables.

3. The calculation result is returned to the external (exit) through the function)

 

Related:

Http://www.vcpc.univie.ac.at/information/mirror/HPFF/hpf1/hpf-v10/subsubsection2_5_3_1_1.html

 

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.