Jump to content

Happy Halloween B-Zone. Detalii aici

Dăm drumul ediției 9 de Miss and Mister B-Zone. Detalii aici

Recrutăm designeri audio și video. Aplicați aici

Sign in to follow this  
CouldnoT

[PASCAL] Procedure and function Overloading

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.

 

Share this post


Link to post
Share on other sites

tc5.png

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×

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.