FUNCTION
A Pascal FUNCTION groups a set of statements under a name and returns a value to the caller. Use a PROCEDURE when no return value is needed. Inside a function, you assign the return value to the special variable Result (or to the function name itself in older-style code).
Syntax
{ Function definition }
function FunctionName(param: Type; ...): ReturnType;
var
localVar: Type; { local variables, if needed }
begin
{ statements }
Result := returnValue; { assigning to Result is common practice }
end;
{ Function call }
variable := FunctionName(arg, ...);
{ Older style: assigning to the function name itself }
function FunctionName(param: Type): ReturnType;
begin
FunctionName := returnValue; { compatible with older Pascal }
end;
Syntax Reference
| Syntax / Keyword | Description |
|---|---|
function Name(...): Type; | Defines a function that returns a value. The definition must appear before the main begin...end. block. |
Result | An implicit variable holding the return value. Writing Result := value; inside the begin block is common practice. |
Assignment to function name FuncName := value; | The older method of specifying the return value. Mixing it with Result in the same function makes the code confusing, so using one style consistently is common practice. |
var (inside a function) | Declares local variables scoped to the function. Written between the function declaration and begin. |
begin...end; | The function body block, ending with a semicolon (;). This is different from the program's final end.. |
Forward declaration forward; | Declares the function name and signature before the body. Used when definition order would otherwise be a problem, such as with mutual recursion. |
Exit; | Returns to the caller immediately from anywhere in the function. Setting Result before calling Exit is common practice. |
Sample Code
jjk_function.pas
{$mode objfpc}
program jjk_function;
uses SysUtils;
const
JUJUTSU_HIGH = 'Jujutsu High';
function GetCurseRank(power_level: Integer): String;
begin
if power_level >= 10000 then begin
Result := 'Special Grade';
end else if power_level >= 5000 then begin
Result := 'Grade 1';
end else if power_level >= 1000 then begin
Result := 'Semi-Grade 1';
end else if power_level >= 500 then begin
Result := 'Grade 2';
end else begin
Result := 'Grade 3 or below';
end;
end;
function CombinePower(a: Integer; b: Integer): Integer;
begin
Result := a + b;
end;
function CanUseMugen(name: String): Boolean;
begin
if name = 'Satoru Gojo' then begin
Result := True;
end else begin
Result := False;
end;
end;
function BuildProfile(name: String; school: String; rank: String): String;
var
profile: String;
begin
profile := '[' + school + '] ' + name + ' — Rank: ' + rank;
Result := profile;
end;
function DescribePower(power_level: Integer): String;
begin
if power_level <= 0 then begin
Result := 'No cursed energy (civilian)';
Exit;
end;
Result := 'Cursed energy present — estimated: ' + IntToStr(power_level);
end;
var
yuji_power : Integer;
megumi_power: Integer;
nobara_power: Integer;
gojo_power : Integer;
nanami_power: Integer;
combined : Integer;
begin
yuji_power := 8500;
megumi_power := 4800;
nobara_power := 1200;
gojo_power := 99999;
nanami_power := 6000;
WriteLn('===== Jujutsu Kaisen FUNCTION Sample =====');
WriteLn('');
WriteLn('--- Curse Rank ---');
WriteLn(' Yuji Itadori (', yuji_power, ') : ', GetCurseRank(yuji_power));
WriteLn(' Megumi Fushiguro (', megumi_power,') : ', GetCurseRank(megumi_power));
WriteLn(' Nobara Kugisaki (', nobara_power,') : ', GetCurseRank(nobara_power));
WriteLn(' Satoru Gojo (', gojo_power, ') : ', GetCurseRank(gojo_power));
WriteLn(' Kento Nanami (', nanami_power,') : ', GetCurseRank(nanami_power));
WriteLn('');
WriteLn('--- Combined Power ---');
combined := CombinePower(yuji_power, megumi_power);
WriteLn(' Yuji + Megumi = ', combined);
WriteLn('');
WriteLn('--- Infinity Technique ---');
WriteLn(' Satoru Gojo : ', CanUseMugen('Satoru Gojo'));
WriteLn(' Yuji Itadori : ', CanUseMugen('Yuji Itadori'));
WriteLn('');
WriteLn('--- Profiles ---');
WriteLn(' ', BuildProfile('Yuji Itadori', JUJUTSU_HIGH, GetCurseRank(yuji_power)));
WriteLn(' ', BuildProfile('Megumi Fushiguro', JUJUTSU_HIGH, GetCurseRank(megumi_power)));
WriteLn(' ', BuildProfile('Nobara Kugisaki', JUJUTSU_HIGH, GetCurseRank(nobara_power)));
WriteLn(' ', BuildProfile('Satoru Gojo', JUJUTSU_HIGH, GetCurseRank(gojo_power)));
WriteLn(' ', BuildProfile('Kento Nanami', JUJUTSU_HIGH, GetCurseRank(nanami_power)));
WriteLn('');
WriteLn('--- Power Description (Early Exit) ---');
WriteLn(' Satoru Gojo : ', DescribePower(gojo_power));
WriteLn(' Civilian : ', DescribePower(0));
WriteLn('==========================================');
end.
fpc jjk_function.pas && ./jjk_function Free Pascal Compiler version ... Linking ./jjk_function ===== Jujutsu Kaisen FUNCTION Sample ===== --- Curse Rank --- Yuji Itadori (8500) : Grade 1 Megumi Fushiguro (4800) : Semi-Grade 1 Nobara Kugisaki (1200) : Semi-Grade 1 Satoru Gojo (99999) : Special Grade Kento Nanami (6000) : Grade 1 --- Combined Power --- Yuji + Megumi = 13300 --- Infinity Technique --- Satoru Gojo : TRUE Yuji Itadori : FALSE --- Profiles --- [Jujutsu High] Yuji Itadori — Rank: Grade 1 [Jujutsu High] Megumi Fushiguro — Rank: Semi-Grade 1 [Jujutsu High] Nobara Kugisaki — Rank: Semi-Grade 1 [Jujutsu High] Satoru Gojo — Rank: Special Grade [Jujutsu High] Kento Nanami — Rank: Grade 1 --- Power Description (Early Exit) --- Satoru Gojo : Cursed energy present — estimated: 99999 Civilian : No cursed energy (civilian) ==========================================
Example 2: Recursive function (factorial)
A function can call itself — this is called recursion. This example calculates the factorial n! to show the basic structure of a recursive function.
jjk_factorial.pas
{$mode objfpc}
program jjk_factorial;
{ Recursive function that returns n! }
function Factorial(n : Integer) : Integer;
begin
if n <= 1 then begin
Result := 1;
end else begin
Result := n * Factorial(n - 1);
end;
end;
begin
WriteLn('--- Factorial Calculation ---');
WriteLn(' 1! = ', Factorial(1));
WriteLn(' 3! = ', Factorial(3));
WriteLn(' 5! = ', Factorial(5));
WriteLn(' 7! = ', Factorial(7));
end.
fpc jjk_factorial.pas && ./jjk_factorial Free Pascal Compiler version ... Linking ./jjk_factorial --- Factorial Calculation --- 1! = 1 3! = 6 5! = 120 7! = 5040
Example 3: Procedure with var parameters (pass by reference)
A parameter marked with the var keyword is passed by reference. Any change made to it inside the procedure is reflected in the caller's variable. This is useful when you want a subroutine to "return" more than one value.
jjk_swap.pas
program jjk_swap;
{ Swaps a and b in place (var = pass by reference) }
procedure Swap(var a : Integer; var b : Integer);
var
tmp : Integer;
begin
tmp := a;
a := b;
b := tmp;
end;
var
yuji_power : Integer;
megumi_power: Integer;
begin
yuji_power := 8500;
megumi_power := 4800;
WriteLn('Before: Yuji=', yuji_power, ' Megumi=', megumi_power);
Swap(yuji_power, megumi_power);
WriteLn('After: Yuji=', yuji_power, ' Megumi=', megumi_power);
end.
fpc jjk_swap.pas && ./jjk_swap Free Pascal Compiler version ... Linking ./jjk_swap Before: Yuji=8500 Megumi=4800 After: Yuji=4800 Megumi=8500
Example 4: Function that returns a record
Pascal functions can return a record type, which is useful when you want to return multiple related values together.
jjk_record_func.pas
{$mode objfpc}
program jjk_record_func;
type
TJujutsushi = record
name : String;
power : Integer;
rank : String;
end;
function MakeJujutsushi(name : String; power : Integer) : TJujutsushi;
begin
Result.name := name;
Result.power := power;
if power >= 10000 then begin
Result.rank := 'Special Grade';
end else if power >= 5000 then begin
Result.rank := 'Grade 1';
end else begin
Result.rank := 'Semi-Grade 1 or below';
end;
end;
var
gojo : TJujutsushi;
yuji : TJujutsushi;
begin
gojo := MakeJujutsushi('Satoru Gojo', 99999);
yuji := MakeJujutsushi('Yuji Itadori', 8500);
WriteLn(gojo.name, ' — Power: ', gojo.power, ' Rank: ', gojo.rank);
WriteLn(yuji.name, ' — Power: ', yuji.power, ' Rank: ', yuji.rank);
end.
fpc jjk_record_func.pas && ./jjk_record_func Free Pascal Compiler version ... Linking ./jjk_record_func Satoru Gojo — Power: 99999 Rank: Special Grade Yuji Itadori — Power: 8500 Rank: Grade 1
Common Mistakes
Forgetting to assign to Result
If a function exits without assigning a value to Result, the return value is undefined. This is especially easy to miss when mixing the old function-name assignment style (Add := a + b) with the Result style.
{ Wrong: a branch exists where Result is never assigned }
function GetRank(power : Integer) : String;
begin
if power >= 10000 then begin
Result := 'Special Grade';
end;
{ when power < 10000, Result is left undefined }
end;
{ Correct: every branch assigns to Result }
function GetRank(power : Integer) : String;
begin
if power >= 10000 then begin
Result := 'Special Grade';
end else begin
Result := 'Grade 1 or below';
end;
end;
Mixing Result assignment and function-name assignment in the same function
Using both Result := x and FunctionName := x inside the same function makes the code harder to read. Choosing one style and applying it consistently is common practice.
{ Wrong: Result style and function-name style are mixed }
function CombinePower(a : Integer; b : Integer) : Integer;
begin
Result := a + b;
CombinePower := Result * 2; { unclear which is the final return value }
end;
{ Correct: use the Result style consistently }
function CombinePower(a : Integer; b : Integer) : Integer;
begin
Result := (a + b) * 2;
end;
Overview
In Pascal, a FUNCTION declares a return type and returns a value by writing Result := value; inside the body. Result is an implicit local variable provided by Free Pascal whose type matches the declared return type. If Result is not assigned before the function exits, the return value is indeterminate. Use Exit; to return to the caller early from anywhere in the function. For subroutines with no return value, see PROCEDURE; for pass-by-reference parameters, see VAR parameters (pass by reference).
If you find any errors or copyright issues, please contact us.