Knowledge (XXG)

Function overloading

Source 📝

131: 1177:
declaration are both included in the overload, with the inner declaration masking the outer declaration only if the signature matches. The first is taken in C++: "in C++, there is no overloading across scopes." As a result, to obtain an overload set with functions declared in different scopes, one needs to explicitly import the functions from the outer scope into the inner scope, with the
36: 77: 1053:
By overloading the constructor, one could pass the tip and total as parameters at creation. This shows the overloaded constructor with two parameters. This overloaded constructor is placed in the class as well as the original constructor we used before. Which one gets used depends on the number of
1330:
If a method is designed with an excessive number of overloads, it may be difficult for developers to discern which overload is being called simply by reading the code. This is particularly true if some of the overloaded parameters are of types that are inherited types of other possible parameters
1176:
If a function is declared in one scope, and then another function with the same name is declared in an inner scope, there are two natural possible overloading behaviors: the inner declaration masks the outer declaration (regardless of signature), or both the inner declaration and the outer
1184:
Implicit type conversion complicates function overloading because if the types of parameters do not exactly match the signature of one of the overloaded functions, but can match after type conversion, resolution depends on which type conversion is chosen.
400:
of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.
939:. Because in many languages the constructor's name is predetermined by the name of the class, it would seem that there can be only one constructor. Whenever multiple constructors are needed, they are to be implemented as overloaded functions. In 951:
with their appropriate default values, "which is normally zero for numeral fields and empty string for string fields". For example, a default constructor for a restaurant bill object written in C++ might set the tip to 15%:
1153:) to make it inaccessible from outside. For the Bill above total might be the only constructor parameter – since a Bill has no sensible default for total – whereas tip defaults to 0.15. 422:, whereas the former does not require a parameter, and is called with an empty parameter field. A common error would be to assign a default value to the object in the second function, which would result in an 591:. An overloaded function is a set of different functions that are callable with the same name. For any particular call, the compiler determines which overloaded function to use and resolves this at 446:
If we write the overloaded print functions for all objects our program will "print", we never have to worry about the type of the object, and the correct function call again, the call is always:
921:
In the above example, the volume of each component is calculated using one of the three functions named "volume", with selection based on the differing number and type of actual parameters.
1149:
Another reason for constructor overloading can be to enforce mandatory data members. In this case the default constructor is declared private or protected (or preferably deleted since
1118:
Now a function that creates a new Bill object could pass two values into the constructor and set the data members in one step. The following shows creation and setting the values:
1001:
The drawback to this is that it takes two steps to change the value of the created Bill object. The following shows creation and changing the values within the main program:
929: 1334:
Type-based overloading can also hamper code maintenance, where code updates can accidentally change which method overload is chosen by the compiler.
1188:
These can combine in confusing ways: An inexact match declared in an inner scope can mask an exact match declared in an outer scope, for instance.
1731: 371: 1569: 148: 49: 1318:
in the derived class being converted to a double and matching the function in the derived class, rather than in the base class; Including
439:
function that executes different actions based on whether it's printing text or photos. The two different functions may be overloaded as
87: 1674: 1606: 232: 214: 112: 63: 195: 575:, i.e. differ in the number or the types of their formal parameters (as in C++) or additionally in their return type (as in Ada). 167: 599: 263: 1348: 1343: 551: 477: 152: 174: 541: 497: 521: 507: 487: 419: 1378: 1331:(for example "object"). An IDE can perform the overload resolution and display (or navigate to) the correct overload. 502: 467: 181: 546: 462: 364: 141: 415: 163: 55: 1482: 1524: 1170: 1166: 568:
The same function name is used for more than one function definition in a particular module, class or namespace
293: 246: 1363: 458:
Languages which support function overloading include, but are not necessarily limited to, the following:
357: 250: 1383: 936: 385: 284: 1403: 944: 531: 307: 270: 1542: 188: 345: 1506: 1670: 1625: 1602: 1565: 1373: 1322:
results in an overload in the derived class and thus matching the function in the base class.
948: 556: 340: 254: 1695: 1647: 1368: 1358: 1353: 603: 580: 536: 330: 325: 302: 933: 335: 1629: 1457: 1691: 1427: 572: 1725: 588: 584: 1643: 1162: 592: 17: 130: 1146:
This can be useful in increasing program efficiency and reducing code length.
526: 512: 397: 316: 1191:
For example, to have a derived class with an overloaded function taking a
427: 1150: 1054:
parameters provided when the new Bill object is created (none, or two):
932:, used to create instances of an object, may also be overloaded in some 492: 482: 516: 1447:, p. 238-244, §Chapter 8 Item 52: Eliminate unchecked warnings. 1587:, p. 238-244, §Chapter 8 Item 52: Use overloading judiciously. 940: 472: 86:
may be in need of reorganization to comply with Knowledge (XXG)'s
1161:
Two issues interact with and complicate function overloading:
124: 70: 29: 595:. This is true for programming languages such as Java. 94: 1630:"Why doesn't overloading work for derived classes?" 1319: 1315: 1311: 1307: 1200: 1196: 1192: 1178: 155:. Unsourced material may be challenged and removed. 602:where the choice is made at runtime, e.g. through 414:are overloaded functions. To call the latter, an 1564:. John Wiley & Sons, Inc. pp. 204–207. 579:Function overloading is usually associated with 1560:Watt, David A.; Findlay, William (1 May 2004). 430:wouldn't know which of the two methods to use. 1203:from the base class, in C++, one would write: 97:to make improvements to the overall structure. 1620: 1618: 947:take no parameters, instantiating the object 365: 8: 1667:"Effective Java: Programming Language Guide" 1483:"Database PL/SQL User's Guide and Reference" 598:Function overloading differs from forms of 442:Print(text_object T); Print(image_object P) 64:Learn how and when to remove these messages 372: 358: 259: 233:Learn how and when to remove this message 215:Learn how and when to remove this message 113:Learn how and when to remove this message 27:Capability of some programming languages 1395: 315: 292: 269: 262: 1703:Journal of Object-Oriented Programming 1599:Learn C# in One Day and Learn It Well 1584: 1444: 1404:"Clojure - Learn Clojure - Functions" 7: 1562:Programming Language Design Concepts 610:Example: Function overloading in C++ 153:adding citations to reliable sources 583:programming languages that enforce 1696:"Overloading vs Object Technology" 1669:(third ed.). Addison-Wesley. 571:The functions must have different 396:is the ability to create multiple 25: 45:This article has multiple issues. 1709:(4). 101 Communications LLC: 3–7 1601:(Revised ed.). p. 82. 454:Languages supporting overloading 129: 75: 34: 1428:"Kotlin language specification" 1199:, using the function taking an 140:needs additional citations for 53:or discuss these issues on the 1349:Constructor (computer science) 1344:Abstraction (computer science) 1: 1732:Method (computer programming) 563:Rules in function overloading 1458:"37.6. Function Overloading" 1379:Object-oriented programming 331:Single and dynamic dispatch 1748: 244: 606:, instead of statically. 1462:PostgreSQL Documentation 1205: 1171:implicit type conversion 1120: 1056: 1003: 954: 700:// Volume of a cylinder. 613: 245:Not to be confused with 1306:Failing to include the 925:Constructor overloading 294:Parametric polymorphism 247:Instruction overlapping 1665:Bloch, Joshua (2018). 1364:Factory method pattern 787:// Volume of a cuboid. 164:"Function overloading" 937:programming languages 433:Another example is a 386:programming languages 251:Overlay (programming) 1646:(3 September 2009). 1597:Chan, Jamie (2017). 1543:"Embarcadero Delphi" 1384:Operator overloading 1314:parameter passed to 945:default constructors 643:// Volume of a cube. 418:must be passed as a 390:function overloading 285:Operator overloading 280:Function overloading 149:improve this article 1648:"Systemic Overload" 532:Visual Basic (.NET) 308:Generic programming 271:Ad hoc polymorphism 95:editing the article 1626:Stroustrup, Bjarne 394:method overloading 346:Predicate dispatch 18:Method overloading 1701:. Eiffel column. 1571:978-0-470-85320-7 1374:Method overriding 604:virtual functions 382: 381: 341:Multiple dispatch 255:Method overriding 243: 242: 235: 225: 224: 217: 199: 123: 122: 115: 88:layout guidelines 68: 16:(Redirected from 1739: 1718: 1716: 1714: 1700: 1694:(October 2001). 1680: 1652: 1651: 1640: 1634: 1633: 1622: 1613: 1612: 1594: 1588: 1582: 1576: 1575: 1557: 1551: 1550: 1539: 1533: 1532: 1529:crystal-lang.org 1521: 1515: 1514: 1503: 1497: 1496: 1494: 1493: 1479: 1473: 1472: 1470: 1469: 1454: 1448: 1442: 1436: 1435: 1424: 1418: 1417: 1415: 1414: 1400: 1369:Method signature 1359:Dynamic dispatch 1354:Default argument 1321: 1317: 1313: 1309: 1302: 1299: 1296: 1293: 1290: 1287: 1284: 1281: 1278: 1275: 1272: 1269: 1266: 1263: 1260: 1257: 1254: 1251: 1248: 1245: 1242: 1239: 1236: 1233: 1230: 1227: 1224: 1221: 1218: 1215: 1212: 1209: 1202: 1198: 1194: 1180: 1142: 1139: 1136: 1133: 1130: 1127: 1124: 1114: 1111: 1108: 1105: 1102: 1099: 1096: 1093: 1090: 1087: 1084: 1081: 1078: 1075: 1072: 1069: 1066: 1063: 1060: 1049: 1046: 1043: 1040: 1037: 1034: 1031: 1028: 1025: 1022: 1019: 1016: 1013: 1010: 1007: 997: 994: 991: 988: 985: 982: 979: 976: 973: 970: 967: 964: 961: 958: 917: 914: 911: 908: 905: 902: 899: 896: 893: 890: 887: 884: 881: 878: 875: 872: 869: 866: 863: 860: 857: 854: 851: 848: 845: 842: 839: 836: 833: 830: 827: 824: 821: 818: 815: 812: 809: 806: 803: 800: 797: 794: 791: 788: 785: 782: 779: 776: 773: 770: 767: 764: 761: 758: 755: 752: 749: 746: 743: 740: 737: 734: 731: 728: 725: 722: 719: 716: 713: 710: 707: 704: 701: 698: 695: 692: 689: 686: 683: 680: 677: 674: 671: 668: 665: 662: 659: 656: 653: 650: 647: 644: 641: 638: 635: 632: 629: 626: 623: 620: 619:<iostream> 617: 581:statically-typed 537:Wolfram Language 449: 448:Print(something) 445: 443: 438: 437: 413: 412: 411:doTask(object o) 407: 374: 367: 360: 326:Virtual function 303:Generic function 260: 238: 231: 220: 213: 209: 206: 200: 198: 157: 133: 125: 118: 111: 107: 104: 98: 79: 78: 71: 60: 38: 37: 30: 21: 1747: 1746: 1742: 1741: 1740: 1738: 1737: 1736: 1722: 1721: 1712: 1710: 1698: 1692:Meyer, Bertrand 1690: 1687: 1677: 1664: 1661: 1656: 1655: 1642: 1641: 1637: 1624: 1623: 1616: 1609: 1596: 1595: 1591: 1583: 1579: 1572: 1559: 1558: 1554: 1547:embarcadero.com 1541: 1540: 1536: 1523: 1522: 1518: 1505: 1504: 1500: 1491: 1489: 1487:docs.oracle.com 1481: 1480: 1476: 1467: 1465: 1456: 1455: 1451: 1443: 1439: 1426: 1425: 1421: 1412: 1410: 1402: 1401: 1397: 1392: 1340: 1328: 1304: 1303: 1300: 1297: 1294: 1291: 1288: 1285: 1282: 1279: 1276: 1273: 1270: 1267: 1264: 1261: 1258: 1255: 1252: 1249: 1246: 1243: 1240: 1237: 1234: 1231: 1228: 1225: 1222: 1219: 1216: 1213: 1210: 1207: 1159: 1144: 1143: 1140: 1137: 1134: 1131: 1128: 1125: 1122: 1116: 1115: 1112: 1109: 1106: 1103: 1100: 1097: 1094: 1091: 1088: 1085: 1082: 1079: 1076: 1073: 1070: 1067: 1064: 1061: 1058: 1051: 1050: 1047: 1044: 1041: 1038: 1035: 1032: 1029: 1026: 1023: 1020: 1017: 1014: 1011: 1008: 1005: 999: 998: 995: 992: 989: 986: 983: 980: 977: 974: 971: 968: 965: 962: 959: 956: 934:object-oriented 927: 919: 918: 915: 912: 909: 906: 903: 900: 897: 894: 891: 888: 885: 882: 879: 876: 873: 870: 867: 864: 861: 858: 855: 852: 849: 846: 843: 840: 837: 834: 831: 828: 825: 822: 819: 816: 813: 810: 807: 804: 801: 798: 795: 792: 789: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 741: 738: 735: 732: 729: 726: 723: 720: 717: 714: 711: 708: 705: 702: 699: 696: 693: 690: 687: 684: 681: 678: 675: 672: 669: 666: 663: 660: 657: 654: 651: 648: 645: 642: 639: 636: 633: 630: 627: 624: 621: 618: 615: 573:type signatures 565: 456: 447: 441: 440: 436:Print(object o) 435: 434: 410: 409: 405: 378: 336:Double dispatch 258: 239: 228: 227: 226: 221: 210: 204: 201: 158: 156: 146: 134: 119: 108: 102: 99: 93:Please help by 92: 80: 76: 39: 35: 28: 23: 22: 15: 12: 11: 5: 1745: 1743: 1735: 1734: 1724: 1723: 1720: 1719: 1686: 1685:External links 1683: 1682: 1681: 1676:978-0134685991 1675: 1660: 1657: 1654: 1653: 1635: 1614: 1608:978-1518800276 1607: 1589: 1577: 1570: 1552: 1534: 1525:"Crystal Docs" 1516: 1498: 1474: 1449: 1437: 1432:kotlinlang.org 1419: 1394: 1393: 1391: 1388: 1387: 1386: 1381: 1376: 1371: 1366: 1361: 1356: 1351: 1346: 1339: 1336: 1327: 1324: 1310:results in an 1206: 1158: 1155: 1121: 1057: 1004: 955: 926: 923: 614: 589:function calls 577: 576: 569: 564: 561: 560: 559: 554: 549: 544: 539: 534: 529: 524: 519: 510: 505: 500: 495: 490: 485: 480: 475: 470: 465: 455: 452: 426:error, as the 424:ambiguous call 380: 379: 377: 376: 369: 362: 354: 351: 350: 349: 348: 343: 338: 333: 328: 320: 319: 313: 312: 311: 310: 305: 297: 296: 290: 289: 288: 287: 282: 274: 273: 267: 266: 241: 240: 223: 222: 137: 135: 128: 121: 120: 83: 81: 74: 69: 43: 42: 40: 33: 26: 24: 14: 13: 10: 9: 6: 4: 3: 2: 1744: 1733: 1730: 1729: 1727: 1708: 1704: 1697: 1693: 1689: 1688: 1684: 1678: 1672: 1668: 1663: 1662: 1658: 1649: 1645: 1644:Bracha, Gilad 1639: 1636: 1631: 1627: 1621: 1619: 1615: 1610: 1604: 1600: 1593: 1590: 1586: 1581: 1578: 1573: 1567: 1563: 1556: 1553: 1548: 1544: 1538: 1535: 1530: 1526: 1520: 1517: 1512: 1508: 1502: 1499: 1488: 1484: 1478: 1475: 1463: 1459: 1453: 1450: 1446: 1441: 1438: 1433: 1429: 1423: 1420: 1409: 1405: 1399: 1396: 1389: 1385: 1382: 1380: 1377: 1375: 1372: 1370: 1367: 1365: 1362: 1360: 1357: 1355: 1352: 1350: 1347: 1345: 1342: 1341: 1337: 1335: 1332: 1325: 1323: 1204: 1189: 1186: 1182: 1174: 1172: 1168: 1164: 1157:Complications 1156: 1154: 1152: 1147: 1119: 1055: 1002: 978:// percentage 953: 950: 946: 942: 938: 935: 931: 924: 922: 612: 611: 607: 605: 601: 596: 594: 590: 586: 585:type checking 582: 574: 570: 567: 566: 562: 558: 555: 553: 550: 548: 545: 543: 540: 538: 535: 533: 530: 528: 525: 523: 520: 518: 514: 511: 509: 506: 504: 501: 499: 496: 494: 491: 489: 486: 484: 481: 479: 476: 474: 471: 469: 466: 464: 461: 460: 459: 453: 451: 431: 429: 425: 421: 417: 404:For example, 402: 399: 395: 391: 387: 375: 370: 368: 363: 361: 356: 355: 353: 352: 347: 344: 342: 339: 337: 334: 332: 329: 327: 324: 323: 322: 321: 318: 314: 309: 306: 304: 301: 300: 299: 298: 295: 291: 286: 283: 281: 278: 277: 276: 275: 272: 268: 265: 261: 256: 252: 248: 237: 234: 219: 216: 208: 197: 194: 190: 187: 183: 180: 176: 173: 169: 166: –  165: 161: 160:Find sources: 154: 150: 144: 143: 138:This article 136: 132: 127: 126: 117: 114: 106: 96: 90: 89: 84:This article 82: 73: 72: 67: 65: 58: 57: 52: 51: 46: 41: 32: 31: 19: 1711:. Retrieved 1706: 1702: 1666: 1638: 1598: 1592: 1580: 1561: 1555: 1546: 1537: 1528: 1519: 1511:nim-lang.org 1510: 1507:"Nim Manual" 1501: 1490:. Retrieved 1486: 1477: 1466:. Retrieved 1464:. 2021-08-12 1461: 1452: 1440: 1431: 1422: 1411:. Retrieved 1407: 1398: 1333: 1329: 1305: 1190: 1187: 1183: 1175: 1163:Name masking 1160: 1148: 1145: 1117: 1052: 1000: 930:Constructors 928: 920: 609: 608: 600:polymorphism 597: 593:compile time 578: 457: 432: 423: 403: 393: 389: 383: 279: 264:Polymorphism 229: 211: 205:October 2011 202: 192: 185: 178: 171: 159: 147:Please help 142:verification 139: 109: 103:October 2011 100: 85: 61: 54: 48: 47:Please help 44: 1650:. Room 101. 1408:clojure.org 724:static_cast 1659:References 1585:Bloch 2018 1492:2021-08-29 1468:2021-08-29 1445:Bloch 2018 1413:2023-06-13 527:TypeScript 513:PostgreSQL 175:newspapers 50:improve it 1713:27 August 1390:Citations 1181:keyword. 706:3.1415926 420:parameter 398:functions 317:Subtyping 56:talk page 1726:Category 1338:See also 1165:(due to 889:<< 859:<< 835:<< 616:#include 428:compiler 406:doTask() 384:In some 1326:Caveats 949:members 552:Crystal 493:Fortran 483:Clojure 189:scholar 1673:  1605:  1568:  1292:double 1262:public 1253:public 1217:public 1195:or an 1193:double 1169:) and 1074:double 1065:double 892:Volume 862:Volume 838:Volume 790:return 751:Volume 730:double 703:return 679:double 673:Volume 670:double 646:return 625:Volume 557:Delphi 542:Elixir 517:PL/SQL 498:Kotlin 416:object 191:  184:  177:  170:  162:  1699:(PDF) 1320:using 1308:using 1268:using 1244:class 1208:class 1179:using 1167:scope 1151:C++11 1104:total 1098:total 1077:total 1039:total 981:total 522:Scala 508:Julia 488:Swift 253:, or 196:JSTOR 182:books 1715:2020 1671:ISBN 1603:ISBN 1566:ISBN 1283:void 1223:void 1138:4.00 1132:0.10 1126:cafe 1123:Bill 1059:Bill 1045:4.00 1033:cafe 1027:0.10 1015:cafe 1009:cafe 1006:Bill 972:0.15 957:Bill 898:100l 886:cout 856:cout 832:cout 817:main 757:long 748:long 733:> 727:< 515:and 503:Java 468:Apex 408:and 168:news 1312:int 1232:int 1201:int 1197:int 1092:tip 1086:tip 1068:tip 1021:tip 987:0.0 966:tip 941:C++ 880:std 868:2.5 850:std 826:std 814:int 775:int 766:int 688:int 631:int 622:int 587:in 547:Nim 473:C++ 463:Ada 392:or 151:by 1728:: 1707:14 1705:. 1628:. 1617:^ 1545:. 1527:. 1509:. 1485:. 1460:. 1430:. 1406:. 1301:}; 1298:); 1274::: 1241:}; 1238:); 1173:. 1141:); 1095:), 975:), 960:() 943:, 913:); 910:15 904:75 883::: 877:); 853::: 847:); 844:10 829::: 820:() 742:); 478:C# 450:. 388:, 249:, 59:. 1717:. 1679:. 1632:. 1611:. 1574:. 1549:. 1531:. 1513:. 1495:. 1471:. 1434:. 1416:. 1316:F 1295:d 1289:( 1286:F 1280:; 1277:F 1271:B 1265:: 1259:{ 1256:B 1250:: 1247:D 1235:i 1229:( 1226:F 1220:: 1214:{ 1211:B 1135:, 1129:( 1113:} 1110:{ 1107:) 1101:( 1089:( 1083:: 1080:) 1071:, 1062:( 1048:; 1042:= 1036:. 1030:; 1024:= 1018:. 1012:; 996:} 993:{ 990:) 984:( 969:( 963:: 916:} 907:, 901:, 895:( 874:8 871:, 865:( 841:( 823:{ 811:} 808:; 805:h 802:* 799:b 796:* 793:l 784:{ 781:) 778:h 772:, 769:b 763:, 760:l 754:( 745:} 739:h 736:( 721:* 718:r 715:* 712:r 709:* 697:{ 694:) 691:h 685:, 682:r 676:( 667:} 664:; 661:s 658:* 655:s 652:* 649:s 640:{ 637:) 634:s 628:( 444:. 373:e 366:t 359:v 257:. 236:) 230:( 218:) 212:( 207:) 203:( 193:· 186:· 179:· 172:· 145:. 116:) 110:( 105:) 101:( 91:. 66:) 62:( 20:)

Index

Method overloading
improve it
talk page
Learn how and when to remove these messages
layout guidelines
editing the article
Learn how and when to remove this message

verification
improve this article
adding citations to reliable sources
"Function overloading"
news
newspapers
books
scholar
JSTOR
Learn how and when to remove this message
Learn how and when to remove this message
Instruction overlapping
Overlay (programming)
Method overriding
Polymorphism
Ad hoc polymorphism
Function overloading
Operator overloading
Parametric polymorphism
Generic function
Generic programming
Subtyping

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