Clever Geek Handbook
📜 ⬆️ ⬇️

Overloading Procedures and Functions

Overloading of procedures and functions - the possibility of using the same routines: procedures or functions in programming languages.

Content

Reason for appearance

In most early programming languages, there was a limitation to simplify the translation process, according to which at the same time more than one procedure with the same name could not be available in the program. In accordance with this limitation, all routines visible at a given point in the program must have different names.

The names and designations of procedures and functions that are part of a programming language cannot be used by a programmer to name their own subprograms.

Implementation

In order to be able to use several variants of a subprogram with the same name, but with a different number of arguments or other types of arguments (that is, with a different signature , since the argument list is part of the signature), subroutine overloading is introduced. Such overloading is possible within the framework of the procedural paradigm , without the use of object-oriented programming.

During translation, the procedures of the same name and functions are controlled so that they differ in signature, since in this case the translator can uniquely determine the call of the desired subprogram.

To eliminate the error of a programmer who accidentally gave a name to a subroutine that is already in use, an additional requirement for writing a keyword is introduced. This is done, for example, in the Delphi language (keyword overload).

Function Overload Rules

Overloaded functions have the same name, but different numbers or types of arguments. This is a type of static polymorphism , in which the question of which of the functions to call is decided by the list of its arguments. This approach is used in statically typed languages ​​that check argument types when calling a function. An overloaded function actually represents several different functions, and the choice of the appropriate one occurs at the compilation stage. Function overloading should not be confused with forms of polymorphism, where the correct method is selected at runtime, for example, by means of virtual functions, and not statically.

Example: function overloads in C ++

  main ()
 {
     cout << volume ( 10 );
     cout << volume ( 2.5 , 8 );
     cout << volume ( 100 , 75 , 15 );
 }
 
 // volume of a cube
 int volume ( int s )
 {
     return ( s * s * s );
 }
 
 // volume of a cylinder
 double volume ( double r , int h )
 {
     return ( 3.14 * r * r * h );
 }
 
 // volume of a cuboid
 long volume ( long l , int b , int h )
 {
     return ( l * b * h );
 }

In the above example, the volume of various components is calculated using calls to different “volume” functions with arguments that differ in data type or amount.

Example: function overloads in the Nim language.

  1 proc overload ( x : int ) =
 2 echo "string int"
 3
 4 proc overload ( x : float ) =
 5 echo "string float"
 6
 7 overload ( 1 ) # prints "string int"
 8 overload ( 1.1 ) # prints "string float"

Constructor Overload

The constructors used to instantiate objects can also be overloaded in some object-oriented programming languages. Due to the fact that in many languages ​​the name of the constructor is predefined by the name of the class, it may seem that only one constructor can exist. Whenever multiple constructors are required, they are implemented as overloaded functions. The default constructor does not accept parameters; the object instance is members with a zero value. [1] For example, the default constructor for a bill object in a restaurant written in C ++ can set the Tip to 15%:

  Bill ()
 { 
     tip = 15.0 ; 
     total = 0.0 ; 
 }

The disadvantage is that it takes two steps to change the value of the Bill object created. The following shows the creation and modification of values ​​within the main program:

  Bill cafe ;
 cafe .  tip = 10.00 ;
 cafe .  total = 4.00 ;

Through the constructor overload, it would be possible to pass a tip on the overall quality of the parameters when creating. The example shows an overloaded constructor with two parameters:

  Bill ( double setTip , double setTotal )
 { 
     tip = setTip ; 
     total = setTotal ; 
 }

Now the function that creates the new Bill object can pass two values ​​to the constructor and set the data elements in one step. The following shows creating and setting values:

  Bill cafe ( 10.00 , 4.00 );

This can be useful to improve program performance and reduce code size.

Cautions

Multiple overloading of a procedure or function can make it difficult for developers to understand which of the overloads is being used.

Resource Capacity

The possibility of overloading the names of procedures and functions in the program is determined by the capabilities of the parser of the compiler and the requirements of the standard language of their writing. The parsing consists in comparing the call of an overloaded function with a specific function (with a specific signature) and does not affect the resource consumption of the program and its execution time.

The size of the compiled program code when using function overloading instead of a function with an arbitrary number of arguments increases (instead of a single procedure with a variable number of arguments, several are compiled for a specific number), but the program performance when calling a procedure that is described as overloaded also increases (type analysis is not performed and other computational operations during program execution). For example, in the C ++ STL library, frequently used functions with a variable number of arguments are replaced by overloads.

See also

  • Procedural Programming
  • Method override
  • Operator Overload
  • Polymorphism (programming)
  • Pattern Matching
  • Partial template specialization
Source - https://ru.wikipedia.org/w/index.php?title=Reset_of_procedures_and_functions&oldid=91673420


More articles:

  • Synchronized Shooting
  • Indirectly Ionizing Radiation
  • Dissimilation (Linguistics)
  • Port Royal Grammar
  • Wanapam
  • Green Syndicalism
  • List of settlements Malovishersky district, Novgorod region
  • Beloglinskoye rural settlement
  • Malovishersky urban settlement
  • Kaffeklubben

All articles

Clever Geek | 2019