Like what
Copy Code code as follows:
, calculate the powers first, calculate the multiplication and division, and then add and subtract
, with parentheses, first of all parentheses, the same level of operation in order from left to right
At this point all programming languages take the computational order of numbers in mathematics. Of course, there are some operators in programming languages that are different from mathematics. What is the order of the operands?
As follows
Copy Code code as follows:
A and B's and
sum = a + b;
, take the value of a from memory
, take the value of B from memory
, the addition operation
Seems to describe the very retarded, of course, this is the case. One might think that the value of B is taken first, then a, and then added. So the final result is the same. That's true. But what if op arithmetic is a function of execution?
sum = a + fun ();
Suppose the fun function only return a number. At this time, whether it is first to take the value of a, and then to fun after the implementation of the value is irrelevant. The end result is the same. Speaking of this, there is still no fresh, tangled place.
But what if fun not only returned the numbers, but also changed a? Like the following JavaScript code
Copy Code code as follows:
var a = 5;
function Fun () {
A = 10;
return 20;
}
var B = a + fun (); The value of B?
The fun function not only returns 20, but also changes the value of a. And A is exactly a part of the add operation. When a takes 5 or 10 to participate in the addition operation? If it is 5 then the value of B is 25, and if it is 10, B is 30. The result in the JavaScript language is 25. But the C language is 30, as follows
Copy Code code as follows:
int a = 5;
int fun () {
A = 10;
return 20;
}
int B = A + fun (); 30
Therefore, only when the function has side effects, the calculation order of the operands will result in different values. Obviously, each language is implemented differently.
JavaScript language, from left to right, a takes 5,fun to return 20 after execution, and finally 5+20. Note that although a takes 5 when participating in this operation, a value has actually changed. As follows
Copy Code code as follows:
var a = 5;
function Fun () {
A = 10;
return 20;
}
var B = a + fun (); The value of B?
alert (a); 10
C language, the first implementation of the Fun,fun to change the value of a, 10, take a for 10 to participate in this "plus" operation, fun return 20. The result is 10+20.
As you can see, whether it's JavaScript or C. The value of a was eventually changed to 10. The difference is that when you participate in the add operation, JavaScript takes the unchanged value of 5, and C takes the changed value of 10.
In the C language, the operation is from left to right. But when a function is counted as a shipment, the function is executed first. If there are side effects of the modified function, take the changed a value to participate in this operation. Regardless of the order of fun and A. The following puts the fun in front, and the result is still 30 in C language.
Copy Code code as follows:
int a = 5;
int fun () {
A = 10;
return 20;
}
int b = Fun () + A; 30
In JavaScript, if fun and a exchange order, the result is not 25.
Copy Code code as follows:
var a = 5;
function Fun () {
A = 10;
return 20;
}
var B = Fun () + A; B is 30
Related:
Side effects of functions