Procedures & Functions Grade 11
User-defined procedures and functions let you break code into reusable, named blocks. This makes programs easier to read, maintain and debug.
What Is a Method?
A method is a named block of code that performs a specific task. Instead of writing the same instructions over and over inside your event handlers, you write them once, give that block a name, and then simply call that name whenever you need the task done. In Delphi we build methods in two forms: procedures and functions.
Think of a method like a recipe card in a cookbook. The recipe is written down once. Whenever you want that dish, you don't rewrite the steps from scratch — you just fetch the card and follow it. A method works exactly the same way: write the steps once, then "fetch" them by name as often as you like.
What Is a Procedure?
A procedure is a named block of code that does a task but does not send a value back to where it was called. It simply carries out its instructions — showing a message, clearing some boxes, drawing on a canvas — and then control returns to the program.
A procedure is like asking someone to "switch off the lights". They go and do the job. You don't expect them to hand you anything back — you just expect the task to be done.
What Is a Function?
A function is a named block of code that does a task and then returns exactly one value back to where it was called. Inside the function you store the answer in the special variable Result, and that answer is handed back so you can use it — store it, display it, or calculate further with it.
A function is like asking someone "How much is the bread?" They go, check, and come back with an answer you can use. A function always brings something back; a procedure does not have to.
Why Do We Break Code Into Methods?
When you are starting out it can feel like extra work to split your program into little named blocks. But experienced programmers do it for very good reasons:
- DRY (Don't Repeat Yourself) – if the same instructions appear in three places and you copy-paste them, you now have to fix bugs in three places. Put them in one method and you fix it once.
- Readability – a well-named method like
CalcAveragetells the reader exactly what is happening, far better than ten lines of arithmetic. - Easier testing and debugging – you can test one small method on its own instead of hunting through a giant block of code.
- Reuse – once written, a method can be called from many different buttons and events.
Each method should do only one job. If a method is trying to do several things at once, that is usually a sign it should be split into smaller methods.
Parameters vs Arguments
A parameter is a placeholder name listed in the method's declaration — it stands for a value the method will receive. An argument is the actual value you pass in when you call the method. In short: parameters live in the declaration; arguments are the real values you supply at the call.
Think of a parameter as a labelled jar called "sName" sitting empty on the shelf when the recipe is written. The argument is the actual "Ms Coetzee" you scoop into that jar when you finally cook. The jar's label is the parameter; what you put in it is the argument.
Example: in DisplayGreeting(sMessage, sName: string) the names sMessage and sName are parameters. When you call DisplayGreeting('Good morning', 'Ms Coetzee'), the strings 'Good morning' and 'Ms Coetzee' are the arguments.
Value Parameters
A value parameter means the method receives a copy of the argument. The method can change that copy freely inside itself, but the original variable back in the calling code stays untouched. This is the default and safest kind of parameter, because the caller never has to worry about its variables being changed unexpectedly.
A value parameter is like giving someone a photocopy of a document. They can scribble all over their copy, but your original is safe at home.
Procedures vs Functions
| Procedure | Function | |
|---|---|---|
| Returns a value? | No (or modifies var parameters) | Yes — always returns exactly one value |
| Called how? | Alone as a statement | As part of another statement (assigned) |
| Keyword | procedure | function |
Procedures
Without Parameters
// Declaration (above the event handlers)
procedure ShowHello;
begin
ShowMessage('Hello!');
end;
// Calling it
procedure TForm1.btnShowClick(Sender: TObject);
begin
ShowHello; // called alone
end;With Parameters
procedure DisplayGreeting(sMessage, sName: string);
begin
ShowMessage(sMessage + ' ' + sName);
end;
// Call with arguments
DisplayGreeting('Good morning', 'Ms Coetzee');Functions
Without Parameters
function GetAnswer: Integer;
begin
Result := 42;
end;
// Must be assigned
iVal := GetAnswer;With Parameters
function AddNumbers(num1, num2: Integer): Integer;
begin
Result := num1 + num2;
end;
// Call
iAnswer := AddNumbers(5, 3); // = 8
lblResult.Caption := IntToStr(AddNumbers(iA, iB));Function Example — Calculate Average
function CalcAverage(iTotal, iCount: Integer): Real;
begin
if iCount = 0 then
Result := 0
else
Result := iTotal / iCount;
end;
// Use it
rAvg := CalcAverage(iSum, iNumStudents);
lblAvg.Caption := FloatToStr(rAvg);Why Use Procedures and Functions?
To bring it all together, here are the practical benefits you gain every time you reach for a method instead of writing one long block of code:
- Break large programs into manageable pieces
- Reuse code from multiple event handlers — no duplication
- Easier to find and fix bugs
- Easier to update — change in one place, works everywhere
- Each method should perform only one task