Knowledge

Side effect (computer science)

Source đź“ť

909: 1463: 1483: 1473: 668:
following causes of side effects through a function call: 1. Performing I/O. 2. Modifying global variables. 3. Modifying local permanent variables (like static variables in C). 4. Modifying an argument passed by reference. 5. Modifying a local variable, either automatic or static, of a function higher up in the function call sequence (usually via a pointer).
71:; or calling other functions with side-effects. In the presence of side effects, a program's behaviour may depend on history; that is, the order of evaluation matters. Understanding and debugging a function with side effects requires knowledge about the context and its possible histories. Side effects play an important role in the design and analysis of 667:
The term Side effect refers to the modification of the nonlocal environment. Generally this happens when a function (or a procedure) modifies a global variable or arguments passed by reference parameters. But here are other ways in which the nonlocal environment can be modified. We consider the
184:
Side effects caused by the time taken for an operation to execute are usually ignored when discussing side effects and referential transparency. There are some cases, such as with hardware timing or testing, where operations are inserted specifically for their temporal side effects e.g.
140:
with hidden side effects is that, if many instructions have side effects on a single piece of state, like condition codes, then the logic required to update that state sequentially may become a performance bottleneck. The problem is particularly acute on some processors designed with
211:
with side effects is idempotent if multiple applications of the subroutine have the same effect on the system state as a single application, in other words if the function from the system state space to itself associated with the subroutine is idempotent in the
163:
Absence of side effects is a necessary, but not sufficient, condition for referential transparency. Referential transparency means that an expression (such as a function call) can be replaced with its value. This requires that the expression is
127:
side effects—instructions that modify parts of the processor state which are not mentioned in the instruction's mnemonic. A classic example of a hidden side effect is an arithmetic instruction that implicitly modifies
55:
if it has any observable effect other than its primary effect of reading the value of its arguments and returning a value to the invoker of the operation. Example side effects include modifying a
823: 149:. Such a processor may require additional control circuitry to detect hidden side effects and stall the pipeline if the next instruction depends on the results of those effects. 1486: 1476: 1048: 1246: 1145: 728: 1128: 17: 506: 602: 830: 703: 1512: 1085: 680: 33: 1517: 1184: 736: 1345: 893: 858: 102: 94: 1507: 1435: 908: 217: 110: 775:. Conference Record of the 20th Annual ACM Symposium on Principles of Programming Languages. pp. 71–84. 1398: 917: 868: 467: 459: 114: 64: 1337: 434:
to the return value of the first application to -3 returns the same value as the first application to -3.
1373: 1292: 325:
was already set to 3 after the first application, and it is still set to 3 after the second application.
1403: 1123: 1466: 994: 816: 1317: 1115: 661: 447: 173: 158: 48: 202: 1277: 1033: 1025: 169: 80: 1420: 1415: 1218: 937: 656: 146: 86: 76: 75:. The degree to which side effects are used depends on the programming paradigm. For example, 1043: 949: 942: 790: 732: 652: 627: 60: 1213: 1208: 1095: 971: 959: 888: 142: 89:
aims to minimize or eliminate side effects. The lack of side effects makes it easier to do
72: 696: 193:. These instructions do not change state other than taking an amount of time to complete. 8: 1225: 1060: 932: 622: 617: 443: 90: 1194: 1135: 1105: 1090: 1055: 954: 898: 853: 786: 607: 133: 56: 1309: 883: 120: 333: 213: 1256: 1155: 1080: 989: 839: 79:
is commonly used to produce side effects, to update a system's state. By contrast,
40: 1360: 1150: 1070: 979: 137: 129: 117:
do not restrict side effects, but it is customary for programmers to avoid them.
1378: 1261: 1189: 1169: 1075: 1038: 1004: 873: 688: 612: 1501: 1065: 999: 863: 329: 165: 1425: 1408: 1251: 878: 98: 83:
is commonly used to report on the state of system, without side effects.
1241: 1100: 591:// b = 3 evaluates to 3, which then casts to true so the loop is infinite 106: 534:
This presents a potential hangup for novice programmers who may confuse
1327: 1322: 321:
to 3 has the same effect on the system state as the first application:
208: 44: 1388: 1204: 984: 798: 454:
is an expression that evaluates to the same value as the expression
1287: 808: 785: 1282: 1140: 442:
One common demonstration of side effect behavior is that of the
1440: 1430: 1350: 27:
Of a function, an additional effect besides returning a value
647:
Spuler, David A.; Sajeev, A. Sayed Muhammed (January 1994).
1393: 1368: 32:"Hidden side effect" redirects here. For similar uses, see 757: 68: 1383: 794: 336:. For instance, consider the following Python program: 101:
and other stateful computations by replacing them with
500:// b = 3 evaluates to 3, which then gets assigned to a 132:(a hidden side effect) while it explicitly modifies a 136:(the intended effect). One potential drawback of an 640: 1499: 764: 649:Compiler Detection of Function Call Side Effects 430:is idempotent because the second application of 317:is idempotent because the second application of 824: 779: 729:"CSc 520 Principles of Programming Languages" 720: 646: 770: 750: 603:Action at a distance (computer programming) 203:Idempotence § Computer science meaning 152: 67:; raising errors or exceptions; performing 1482: 1472: 831: 817: 771:Jones, Simon Peyton; Wadler, Phil (1993). 673: 176:for the same input) and side-effect free. 789:; Findler, Robert Bruce; Flatt, Matthew; 685:Research Topics in Functional Programming 660: 332:is idempotent if it is idempotent in the 726: 179: 168:, that is to say the expression must be 216:. For instance, consider the following 14: 1500: 679: 458:, with the side effect of storing the 105:actions. Functional languages such as 93:of a program. The functional language 812: 727:Collberg, Christian S. (2005-04-22). 191:for (int i = 0; i < 10000; ++i) {} 838: 697:"Why Functional Programming Matters" 474:. This allows multiple assignment: 24: 731:. Department of Computer Science, 694: 25: 1529: 773:Imperative Functional Programming 1481: 1471: 1462: 1461: 907: 97:eliminates side effects such as 34:hidden variable (disambiguation) 709:from the original on 2022-06-14 196: 18:Side-effect (computer science) 13: 1: 633: 123:programmers must be aware of 560:// tests if b evaluates to 3 7: 1513:Programming language theory 1185:Curry–Howard correspondence 596: 10: 1534: 437: 200: 156: 31: 1457: 1359: 1336: 1308: 1301: 1270: 1234: 1177: 1168: 1114: 1024: 1017: 970: 925: 916: 905: 846: 795:"How To Design Programs" 567: 536: 511: 509:, this is equivalent to 476: 338: 222: 159:Referential transparency 153:Referential transparency 1034:Abstract interpretation 81:declarative programming 1518:Functional programming 791:Krishnamurthi, Shriram 172:(always give the same 147:out-of-order execution 87:Functional programming 77:imperative programming 63:or a mutable argument 943:Categorical semantics 733:University of Arizona 653:James Cook University 628:Unspecified behaviour 505:Because the operator 180:Temporal side effects 145:(since 1990) or with 73:programming languages 61:static local variable 1508:Computer programming 889:Runtime verification 1146:Invariant inference 894:Safety and liveness 787:Felleisen, Matthias 758:"Haskell 98 report" 623:Undefined behaviour 618:Side-channel attack 444:assignment operator 91:formal verification 65:passed by reference 1310:Constraint solvers 1136:Concolic execution 1091:Symbolic execution 899:Undefined behavior 854:Control-flow graph 334:mathematical sense 214:mathematical sense 57:non-local variable 51:is said to have a 1495: 1494: 1453: 1452: 1449: 1448: 1164: 1163: 1013: 1012: 691:. pp. 17–42. 450:. The assignment 121:Assembly language 16:(Redirected from 1525: 1485: 1484: 1475: 1474: 1465: 1464: 1361:Proof assistants 1306: 1305: 1175: 1174: 1022: 1021: 995:Rewriting system 990:Process calculus 923: 922: 911: 840:Program analysis 833: 826: 819: 810: 809: 803: 802: 783: 777: 776: 768: 762: 761: 754: 748: 747: 745: 744: 735:. Archived from 724: 718: 717: 715: 714: 708: 701: 692: 681:Turner, David A. 677: 671: 670: 664: 644: 592: 589: 586: 583: 580: 577: 574: 571: 561: 558: 555: 552: 549: 546: 543: 540: 530: 527: 524: 521: 518: 515: 507:right associates 501: 498: 495: 492: 489: 486: 483: 480: 473: 465: 457: 453: 433: 429: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 324: 320: 316: 310: 307: 304: 301: 298: 295: 292: 289: 286: 283: 280: 277: 274: 271: 268: 265: 262: 259: 256: 253: 250: 247: 244: 241: 238: 235: 232: 229: 226: 192: 188: 43:, an operation, 41:computer science 21: 1533: 1532: 1528: 1527: 1526: 1524: 1523: 1522: 1498: 1497: 1496: 1491: 1445: 1355: 1332: 1297: 1271:Data structures 1266: 1230: 1160: 1151:Program slicing 1110: 1009: 980:Lambda calculus 966: 912: 903: 864:Hyperproperties 842: 837: 807: 806: 784: 780: 769: 765: 756: 755: 751: 742: 740: 725: 721: 712: 710: 706: 699: 678: 674: 645: 641: 636: 608:Don't-care term 599: 594: 593: 590: 587: 584: 581: 578: 575: 572: 569: 563: 562: 559: 556: 553: 550: 547: 544: 541: 538: 532: 531: 528: 525: 522: 519: 516: 513: 503: 502: 499: 496: 493: 490: 487: 484: 481: 478: 471: 463: 455: 451: 440: 431: 427: 425: 424: 421: 418: 415: 412: 409: 406: 403: 400: 397: 394: 391: 388: 385: 382: 379: 376: 373: 370: 367: 364: 361: 358: 355: 352: 349: 346: 343: 340: 322: 318: 314: 312: 311: 308: 305: 302: 299: 296: 293: 290: 287: 284: 281: 278: 275: 272: 269: 266: 263: 260: 257: 254: 251: 248: 245: 242: 239: 236: 233: 230: 227: 224: 205: 199: 190: 186: 182: 161: 155: 138:instruction set 130:condition codes 37: 28: 23: 22: 15: 12: 11: 5: 1531: 1521: 1520: 1515: 1510: 1493: 1492: 1490: 1489: 1479: 1469: 1458: 1455: 1454: 1451: 1450: 1447: 1446: 1444: 1443: 1438: 1433: 1428: 1423: 1418: 1413: 1412: 1411: 1401: 1396: 1391: 1386: 1381: 1376: 1371: 1365: 1363: 1357: 1356: 1354: 1353: 1348: 1342: 1340: 1334: 1333: 1331: 1330: 1325: 1320: 1314: 1312: 1303: 1299: 1298: 1296: 1295: 1290: 1285: 1280: 1274: 1272: 1268: 1267: 1265: 1264: 1259: 1254: 1249: 1244: 1238: 1236: 1232: 1231: 1229: 1228: 1223: 1222: 1221: 1211: 1202: 1197: 1192: 1190:Loop invariant 1187: 1181: 1179: 1172: 1170:Formal methods 1166: 1165: 1162: 1161: 1159: 1158: 1153: 1148: 1143: 1138: 1133: 1132: 1131: 1129:Taint tracking 1120: 1118: 1112: 1111: 1109: 1108: 1103: 1098: 1093: 1088: 1083: 1078: 1076:Model checking 1073: 1068: 1063: 1058: 1053: 1052: 1051: 1041: 1036: 1030: 1028: 1019: 1015: 1014: 1011: 1010: 1008: 1007: 1005:Turing machine 1002: 997: 992: 987: 982: 976: 974: 968: 967: 965: 964: 963: 962: 957: 947: 946: 945: 935: 929: 927: 920: 914: 913: 906: 904: 902: 901: 896: 891: 886: 884:Rice's theorem 881: 876: 874:Path explosion 871: 866: 861: 856: 850: 848: 844: 843: 836: 835: 828: 821: 813: 805: 804: 797:(2 ed.). 793:(2014-08-01). 778: 763: 749: 719: 695:Hughes, John. 689:Addison-Wesley 683:, ed. (1990). 672: 662:10.1.1.70.2096 638: 637: 635: 632: 631: 630: 625: 620: 615: 613:Sequence point 610: 605: 598: 595: 568: 537: 512: 477: 439: 436: 339: 223: 201:Main article: 198: 195: 181: 178: 157:Main article: 154: 151: 26: 9: 6: 4: 3: 2: 1530: 1519: 1516: 1514: 1511: 1509: 1506: 1505: 1503: 1488: 1480: 1478: 1470: 1468: 1460: 1459: 1456: 1442: 1439: 1437: 1434: 1432: 1429: 1427: 1424: 1422: 1419: 1417: 1414: 1410: 1407: 1406: 1405: 1402: 1400: 1397: 1395: 1392: 1390: 1387: 1385: 1382: 1380: 1377: 1375: 1372: 1370: 1367: 1366: 1364: 1362: 1358: 1352: 1349: 1347: 1344: 1343: 1341: 1339: 1335: 1329: 1326: 1324: 1321: 1319: 1316: 1315: 1313: 1311: 1307: 1304: 1300: 1294: 1291: 1289: 1286: 1284: 1281: 1279: 1276: 1275: 1273: 1269: 1263: 1260: 1258: 1255: 1253: 1250: 1248: 1247:Incorrectness 1245: 1243: 1240: 1239: 1237: 1233: 1227: 1224: 1220: 1217: 1216: 1215: 1214:Specification 1212: 1210: 1206: 1203: 1201: 1198: 1196: 1193: 1191: 1188: 1186: 1183: 1182: 1180: 1176: 1173: 1171: 1167: 1157: 1154: 1152: 1149: 1147: 1144: 1142: 1139: 1137: 1134: 1130: 1127: 1126: 1125: 1122: 1121: 1119: 1117: 1113: 1107: 1104: 1102: 1099: 1097: 1094: 1092: 1089: 1087: 1084: 1082: 1079: 1077: 1074: 1072: 1069: 1067: 1066:Effect system 1064: 1062: 1059: 1057: 1054: 1050: 1047: 1046: 1045: 1042: 1040: 1037: 1035: 1032: 1031: 1029: 1027: 1023: 1020: 1016: 1006: 1003: 1001: 1000:State machine 998: 996: 993: 991: 988: 986: 983: 981: 978: 977: 975: 973: 969: 961: 958: 956: 953: 952: 951: 948: 944: 941: 940: 939: 936: 934: 931: 930: 928: 924: 921: 919: 915: 910: 900: 897: 895: 892: 890: 887: 885: 882: 880: 877: 875: 872: 870: 867: 865: 862: 860: 857: 855: 852: 851: 849: 845: 841: 834: 829: 827: 822: 820: 815: 814: 811: 800: 796: 792: 788: 782: 774: 767: 759: 753: 739:on 2022-08-06 738: 734: 730: 723: 705: 698: 690: 686: 682: 676: 669: 663: 658: 654: 650: 643: 639: 629: 626: 624: 621: 619: 616: 614: 611: 609: 606: 604: 601: 600: 566: 535: 510: 508: 475: 469: 461: 449: 445: 435: 337: 335: 331: 330:pure function 326: 221: 219: 215: 210: 204: 194: 177: 175: 171: 170:deterministic 167: 160: 150: 148: 144: 139: 135: 131: 126: 122: 118: 116: 112: 108: 104: 100: 96: 92: 88: 84: 82: 78: 74: 70: 66: 62: 58: 54: 50: 46: 42: 35: 30: 19: 1409:Isabelle/HOL 1226:Verification 1209:completeness 1199: 1101:Type systems 1044:Control flow 938:Denotational 879:Polyvariance 847:Key concepts 781: 772: 766: 752: 741:. Retrieved 737:the original 722: 711:. Retrieved 684: 675: 666: 648: 642: 564: 533: 504: 441: 426: 327: 313: 206: 183: 162: 124: 119: 85: 52: 38: 29: 1338:Lightweight 1200:Side effect 1096:Termination 950:Operational 859:Correctness 197:Idempotence 187:sleep(5000) 107:Standard ML 53:side effect 1502:Categories 1293:Union-find 1257:Separation 1195:Refinement 1061:Dependence 960:Small-step 869:Invariants 743:2022-08-06 713:2022-08-06 634:References 209:subroutine 143:pipelining 49:expression 1389:HOL Light 1219:Languages 1205:Soundness 1124:Data-flow 1106:Typestate 1056:Data-flow 985:Petri net 933:Axiomatic 918:Semantics 799:MIT Press 657:CiteSeerX 466:into the 220:program: 1487:Glossary 1467:Category 1404:Isabelle 1288:Hashcons 1262:Temporal 1178:Concepts 1018:Analyses 955:Big-step 704:Archived 597:See also 134:register 45:function 1477:Outline 1283:E-graph 1156:Testing 1141:Fuzzing 1116:Dynamic 1081:Pointer 760:. 1998. 468:L-value 460:R-value 438:Example 103:monadic 95:Haskell 1252:Linear 1235:Logics 1071:Escape 1026:Static 972:Models 659:  383:assert 356:return 300:assert 276:assert 249:global 218:Python 125:hidden 111:Scheme 1441:Twelf 1431:NuPRL 1426:Mizar 1399:Idris 1346:Alloy 1302:Tools 1242:Hoare 1086:Shape 1039:Alias 926:Types 707:(PDF) 700:(PDF) 570:while 565:with 539:while 452:a = b 174:value 115:Scala 1421:LEGO 1416:Lean 1394:HOL4 1374:Agda 1369:ACL2 1351:TLA+ 1207:and 1049:kCFA 693:Via 377:else 371:< 319:setx 315:setx 288:setx 264:setx 237:setx 166:pure 113:and 59:, a 1436:PVS 1379:Coq 1328:SMT 1323:SAT 1318:CHC 1278:BDD 470:of 462:of 446:in 432:abs 428:abs 410:abs 392:abs 386:abs 344:abs 341:def 234:def 189:or 99:I/O 69:I/O 47:or 39:In 1504:: 1384:F* 702:. 687:. 665:. 655:. 651:. 588:{} 557:{} 548:== 497:); 407:== 404:)) 365:if 353:): 328:A 306:== 282:== 246:): 207:A 109:, 832:e 825:t 818:v 801:. 746:. 716:. 585:) 582:3 579:= 576:b 573:( 554:) 551:3 545:b 542:( 529:; 526:3 523:= 520:b 517:= 514:a 494:3 491:= 488:b 485:( 482:= 479:a 472:a 464:b 456:b 448:C 422:) 419:3 416:- 413:( 401:3 398:- 395:( 389:( 380:n 374:0 368:n 362:n 359:- 350:n 347:( 323:x 309:3 303:x 297:) 294:3 291:( 285:3 279:x 273:) 270:3 267:( 261:n 258:= 255:x 252:x 243:n 240:( 231:0 228:= 225:x 36:. 20:)

Index

Side-effect (computer science)
hidden variable (disambiguation)
computer science
function
expression
non-local variable
static local variable
passed by reference
I/O
programming languages
imperative programming
declarative programming
Functional programming
formal verification
Haskell
I/O
monadic
Standard ML
Scheme
Scala
Assembly language
condition codes
register
instruction set
pipelining
out-of-order execution
Referential transparency
pure
deterministic
value

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

↑