Skip to content
Hub Nexus

No author yetClaim it

Version 1Updated 3 min read

Support

Computing » Computer Science » Computer Programming » Ada Programming

Description

Function overloading (also method overloading) is a programming concept that allows programmers to define two or more functions with the same name and in the same scope.

Each function has a unique signature (or header), which is derived from:

  • function/procedure name
  • number of arguments
  • arguments' type
  • arguments' order
  • arguments' name
  • return type

Please note: Not all above signature options are available in all programming languages.

Language123456
Adayesyesyesyesyesyes
C++yesyesyesyesnono
Javayesyesyesyesnono
Swiftyesyesyesyesyesno

Warning: Function overloading is often confused with function overriding. In Function overloading, a function with a different signature is created, adding to the pool of available functions. In function overriding, however, a function with the same signature is declared, replacing the old function in the context of the new function.

Demonstration

Since functions' names are in this case the same, we must preserve uniqueness of signatures, by changing something from the parameter list (last three alienees).

If the functions' signatures are sufficiently different, the compiler can distinguish which function was intended to be used at each occurrence. This process of searching for the appropriate function is called function resolution and can be quite an intensive one, especially if there are a lot of equally named functions.

Programming languages supporting implicit type conventions usually use promotion of arguments (i.e. type casting of integer to floating-point) when there is no exact function match. The demotion of arguments is rarely used.

When two or more functions match the criteria in function resolution process, an ambiguity error is reported by compiler. Adding more information for the compiler by editing the source code (using for example type casting), can address such doubts.

The example code shows how function overloading can be used. As functions do practically the same thing, it makes sense to use function overloading.

File: function_overloading.adb (view, plain text, download page, browse all)

function Generate_Number (MaxValue : Integer)

G : Random_Pack.Generator;
begin
Random_Pack.Reset (G);
return Random_Pack.Random (G);
end Generate_Number;

function Generate_Number (MinValue : Integer;
MaxValue : Integer) return Integer
is
subtype Random_Type is Integer range MinValue .. MaxValue;
package Random_Pack is new Ada.Numerics.Discrete_Random (Random_Type);

G : Random_Pack.Generator;
begin
Random_Pack.Reset (G);
return Random_Pack.Random (G);
end Generate_Number;

calling the first function

The first code block will generate numbers from 0 up to specified parameter MaxValue. The appropriate function call is:

Number_1 : Integer := Generate_Number (10);

calling the second function

The second requires another parameter MinValue. Function will return numbers above or equals MinValue and lower than MaxValue.

Number_2 : Integer := Generate_Number (6, 10);

Function overloading in Ada

Ada supports all six signature options, but if you use the arguments' name as option, you will always have to name the parameter when calling the function. i.e.:

Number_2 : Integer := Generate_Number (MinValue => 6,
MaxValue => 10);

Note that you cannot overload a generic procedure or generic function within the same package. The following example will fail to compile:

package myPackage
generic
type Value_Type is (<>);
-- The first declaration of a generic subprogram
-- with the name "Generic_Subprogram"
procedure Generic_Subprogram (Value : in out Value_Type);
...
generic
type Value_Type is (<>);
-- This subprogram has the same name, but no
-- input or output parameters. A non-generic
-- procedure would be overloaded here.
-- Since this procedure is generic, overloading
-- is not allowed and this package will not compile.
procedure Generic_Subprogram;
...
generic
type Value_Type is (<>);
-- The same situation.
-- Even though this is a function and not
-- a procedure, generic overloading of
-- the name "Generic_Subprogram" is not allowed.
function Generic_Subprogram (Value : Value_Type) return Value_Type;
end myPackage;

See also

Wikibook

Ada Programming

Ada 95 Reference Manual

Ada 2005 Reference Manual

Where this page came from

This page was imported from Wikibooks. From “Ada Programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

Comments

Spinner Logo

Next in Ada Programming

CC BY-SA 4.0
This computer programming article is available for [pseudocode]( and Ada. [Computing]( » [Computer Science]( » [Computer Programming]( » [Ada Programming]( Ada is very well suited for all kinds of calculations. You can define your own fixed point and floating point types and — with the aid of generi
CC BY-SA 4.0
This computer programming article is available in [pseudocode]( Ada, [C]( [C++]( and [Delphi]( [Computing]( » [Computer Science]( » [Computer Programming]( » [Ada Programming]( Note: there are some simplifications in the explanations below. Don't take anything too literally. Most programming languag
CC BY-SA 4.0
This computer programming article is available in [pseudocode]( and Ada. [Computing]( » [Computer Science]( » [Computer Programming]( » [Ada Programming]( Variables are references that stand in for a value that is contained at a certain memory address. Variables are said to have a value and may have
CC BY-SA 4.0
[Computing]( » [Computer Science]( » [Computer Programming]( » [Ada Programming]( ## Character set The character set used in Ada programs is composed of: - Upper-case letters: A, ..., Z and lower-case letters: a, ..., z. - Digits: 0, ..., 9. - Special characters. Take into account that in Ada 95 the
CC BY-SA 4.0
[Computing]( » [Computer Science]( » [Computer Programming]( » [Ada Programming]( ## Single character delimiters & ampersand (operator) ' apostrophe, tick ( left parenthesis ) right parenthesis asterisk, multiply (operator) + plus sign (operator) , comma hyphen, minus (operator) . full stop, point,
CC BY-SA 4.0
[Computing]( » [Computer Science]( » [Computer Programming]( » [Ada Programming]( ## Standard operators [Ada]( allows [operator overloading]( for all standard operators and so the following summaries can only describe the suggested standard operations for each operator. It is quite possible to misus