Functions are just like procedures, with one main exception. The exception is that functions return a value. We have already used some built-in functions like UpperCase(), so, we will make our own.



The function can take a value that is passed to it. The value passed must be the same data type that is declared in the function.


This example has two functions and one procedure:



program Project1;


{$mode objfpc}{$H+}


uses

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


var

  number:integer;


procedure StopProgram;

begin

  writeln;

  writeln;

  writeln('Press <Enter> To Quit');

  readln;

end;


function squared(var x:integer):integer;

begin

  squared := x * x;

end;


function abs (var x:integer):integer;

begin


  if x >= 0 then

    abs := x;

  else

    abs := x * -1;


end;  {function abs}


begin  {main program}

  write('Enter An Integer: ');

  readln(number);

  writeln('The Number Squared Is: ', squared(number));

  writeln('The Absolute Value Is: ', abs(number));

  StopProgram;

end.



Video Tutorial: Free Pascal Tutorial 12 - Functions - Lazarus (6:10)



Previous                   Next