Jump to content

[PASCAL] Procedure and function Overloading


CouldnoT
 Share

Recommended Posts

Procedure and function Overloading

 

Overloading mean we can write two or more procedures/functions with the same name but different parameters. Different parameters means different parameter types or a different number of parameters. For example, we may need to write two versions of the Sum function, where the first one accepts and returns integer numbers, and the second one accepts and returns real numbers:

 

program sum;
{$mode objfpc}{$H+}
uses
 {$IFDEF UNIX}{$IFDEF UseCThreads}
 cthreads,
 {$ENDIF}{$ENDIF}
 Classes;

function Sum(x, y: Integer): Integer; overload;
begin
    Result:= x + y;
end;
function Sum(x, y: Double): Double; overload;
begin
    Result:= x + y;
end;
var
    j, i: Integer;
    h, g: Double;
begin
    j:= 15;
    i:= 20;
    Writeln(J, ' + ', I, ' = ', Sum(j, i));
    h:= 2.5;
    g:= 7.12;
    Writeln(H, ' + ', G, ' = ', Sum(h, g));
    Write('Press enter key to close');
    Readln;
end.

 

Notice that we have used the reserved word overload, which means: this function is overloaded.

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.