You are on page 1of 9

Mr Long Grade:

Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos

Mr L ong ON SUBPROGRAMS

Subprograms
• Subprograms are a sequence of instructions that perform a specific task when
they are called, and they can be called repeatedly.
• They can be either a function or a procedure.
• When a subprogram is created, it can receive data (referred to as parameters).
• When the subprogram is called, the values or variables used for the data is
referred to as arguments.

Functions
• Subprograms that returns ONE result.
• Name is unique and is always called as part of another statement.
Examples of functions already in Delphi
o iNum := StrToInt ( edtNumber.Text ) ;
StrToInt function takes in a string parameter and returns an integer value
o sTemp := Copy ( sName, 1 , 4 ) ;
Copy function takes in a string and 2 integer parameters and returns a string value
o if odd ( iNum ) then
Odd function takes in an integer parameter and returns a Boolean value

Procedures
• Subprograms that can return no result, one result or many results.
• Name is unique and is always called on its own and not part of another statement.
Examples of procedures already in Delphi
o Showmessage ( ‘Hello’ ) ;
Showmessage procedure takes in a string parameter and displays the string
o Delete ( sName, 1 , 4 ) ;
Delete procedure takes in a string and 2 integer parameters and changes the string value
o Inc ( iNum ) then
Inc procedure takes in an integer parameter and changes the integer value

1
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos

Creating your own subprograms


Creating a custom function
THREE steps when creating a function:
• Step 1 – Declare the function
o At the top of the unit file, under type you can declare your function under
the private (function can only be used in this unit) or public (function can be
used in other units and classes) heading.
General Structure – Function Declaration

function <function name> ( parameters ) : return data type ;

o Function name must be unique, no spaces, start with a letter and not be a
predefined Delphi word.
o Parameters are listed like variables declarations
▪ Parameter name(s) followed by a colon followed by data type
▪ Parameters of different data types are separated by semi-colon.
Example: ( iNum, iValue : integer ; sTemp : string )
o Data type of the result being returned is specified after the colon at the end.
o Examples of function declarations:
▪ function Max3 ( iNum1, iNum2, iNum3 : integer ) : integer ;
Max3 function takes in 3 integer parameters and returns an integer value
▪ function Combine ( sName : string ; iAge : integer ) : string ;
Combine function takes in a string & integer parameter and returns a string value

• Step 2 – Write the code


o Once you have declared your function correctly, Press Ctrl + Shift + C
o If your function was declared correctly, this keystroke combination will
automatically create the code skeleton under implementation for you to
write the instructions that will be executed when the function is called.
o Believe that the parameters will contain values and use them as a type of
“input” of information to be used by the function.
o You may declare local variables inside the function to help with your
calculations.
o Your final answer must be stored in a variable called result OR in a variable
with the same name as the function name.
▪ DO NOT declare the result or function name variable.
▪ If returning a Boolean value, make sure to cater for when result is
TRUE and for when it is FALSE.

2
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos

Example 1 – Step 2
function TForm1.Max3 ( iNum1, iNum2, iNum3 : integer ) : integer ;
begin
if (iNum1 > iNum2) AND (iNum1 > iNum3) then
result := iNum1
else if (iNum2 > iNum1) AND (iNum2 > iNum3) then
result := iNum2
else result := iNum3 ;
end ;

Explanation:
Function Max3 takes in THREE integer values (iNum1, iNum2, iNum3) and returns which one
of those three values is the biggest.
NOTE: Parameters iNum1, iNum2 and iNum3 are used as if they contain values as a form of “input”.
Result variable is used to store value that must be returned. Results variable is NOT declared.

• Step 3 – Use the function


o Once you have written the code then you can call the function whenever
you want those instructions to be executed.
o The function’s instructions will only be executed if the function is called.
o The function must be called with the correct number of arguments (for the
parameters), in the correct order and of the correct data type for each
parameter.
o If using variables for the arguments, the names of the variables DO NOT
need to be the same as those used in the declaration. They can be but they
don’t have to be.

Example Code – Using Max3 function

var iN1, iN2, iN3, iBiggest : integer ;


begin
iN1 := spnNumber1.Value ; //Get input of 3 different numbers from three different
iN2 := spnNumber2.Value ; //spin edit controls into integer variables iN1, iN2 and iN3
iN3 := spnNumber3.Value ;

iBiggest := Max3 ( iN1, iN2, iN3 ) ; //Call Max3 function with iN1, iN2 and iN3 as parameters
//and result is returned into the iBiggest integer variable

showmessage( ‘The biggest number is ‘ + IntToStr( iBiggest ) ) ; // Display biggest number


end;

3
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos

Creating a custom procedure


THREE steps when creating a procedure:
NOTE: Similar steps as mentioned with functions. Differences are highlighted.
• Step 1 – Declare the procedure
o At the top of the unit file, under type you can declare your procedure under
the private (procedure can only be used in this unit) or public (procedure
can be used in other units and classes) heading.
General Structure – Procedure Declaration

procedure <procedure name> ( parameters ) ;

o Procedure name must be unique, no spaces, start with a letter and not be a
predefined Delphi word.
o Parameters are listed like variables declarations
▪ Possible to have procedures with no parameters.
▪ Parameter name(s) followed by a colon followed by data type
▪ Parameters of different data types are separated by semi-colon.
Example: ( iNum, iValue : integer ; sTemp : string )
o No colon or data type at the end.
o Examples of procedures declarations:
▪ procedure DisplayHeading ;
▪ procedure DisplayCustomHeading ( sHeading : string ) ;

• Step 2 – Write the code


o Once you have declared your procedure correctly, Press Ctrl + Shift + C
o If your procedure was declared correctly, this keystroke combination will
automatically create the code skeleton under implementation for you to
write the instructions that will be executed when the procedure is called.
o Believe that the parameters will contain values and use them as a type of
“input” of information to be used by the procedure.
o You may declare local variables inside the procedure to help with your
calculations.
o No result variable or procedure name used as a variable.

4
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos

Example 2 – Step 2
procedure TForm1.DisplayHeading ;
begin
memo1.Clear ;
memo1.Lines.Add( ‘Details Displayed’ ) ;
memo1.Lines.Add(‘ ========= ‘ ) ;
end ;

Explanation:
Procedure DisplayHeading takes in NO values and sets up a memo1 memo control by clearing
the memo control and displaying the words “Details Displayed” and a double line in the
component.

Example 3 – Step 2
procedure TForm1.DisplayCustomHeading ( sHeading : string ) ;
begin
memo1.Clear ;
memo1.Lines.Add( sHeading ) ;
memo1.Lines.Add(‘ ========= ‘ ) ;
end ;

Explanation:
Procedure DisplayCustomHeading takes in a string value parameter and sets up a memo1
memo control by clearing the memo control and displaying the whatever value is in the string
parameter and a double line in the component.
NOTE: Parameter sHeading is used as if it contains values as a form of “input”.

• Step 3 – Use the procedure


o Once you have written the code then you can call the procedure whenever
you want those instructions to be executed.
o The procedure’s instructions will only be executed if the procedure is called.
o The procedure must be called with the correct number of arguments (for
the parameters), in the correct order and of the correct data type for each
parameter.
o If using variables for the arguments, the names of the variables DO NOT
need to be the same as those used in the declaration. They can be but they
don’t have to be.

5
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos

Example Code – Using procedures

begin
sTitle := InputBox( ‘’, ‘Enter title or heading:’, ‘’ ) ; //Get heading to be displayed

if length( sTitle ) = 0 then //If entered title is blank / length is 0


DisplayHeading //Call the DsplayHeading procedure
else
DisplayCustomHeading ( sTitle ) ; //Call the DisplayCustomHeading procedure with sTitle as
//the parameter
end;

Reference parameters in procedures


• Changing the value of a parameter inside a procedure will not result in a change in
the variable that was used as an argument that represents that parameter.
• Consider the following AddFive procedure as an example:

Example 4 – AddFive procedure


procedure TForm1.AddFive ( iNumber : integer ) ;
begin
iNumber := iNumber + 5 ;
end ;

Explanation:
Procedure AddFive takes in an integer value parameter and adds 5 to it (ONLY IN THE
PROCEDURE).

Example Code – Using AddFive procedure

begin
iNum := 51 ; //Give iNum a value of 51
showmessage( IntToStr( iNum ) ) ; //First Display – display iNum before procedure call

AddFive ( iNum ) ; //Call AddFive procedure with iNum as a parameter

showmessage( IntToStr( iNum ) ) ; //Second Display – display iNum after procedure call
end;

6
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos

Notes about the AddFive Example


• iNum is given a value of 51.
• iNum is displayed using a showmessage. (Value of 51 is displayed to user)
• AddFive procedure is called with iNum as a parameter.
o Go to AddFive procedure code.
o Value in the parameter iNumber is the same as iNum given as an argument
(iNumber has a value of 51)
o The procedure adds 5 onto iNumber (iNumber has a value of 56)
o Procedure ends, return to example code
• iNum is displayed using a showmessage. (Value of 51 is displayed to user)

Because iNum is called as an argument for AddFive procedure, iNumber and iNum
will both have the value of 51. However, just because the value of iNumber is
changed inside the procedure, does NOT mean the value of iNum changes as well.
When iNum is displayed after the procedure call, the value of iNum has not changed
and remains 51.

If you want the variables iNum and iNumber to be linked in a way that if iNumber is
changed in the procedure, that change will be reflected back to iNum, then you must
make iNumber in the procedure a reference parameter.
o This is done by placing the var statement in front of any variables that will
reference parameter in the declaration.
o This is the equivalent of returning a result like a function.
o Multiple parameters can be reference parameters so therefore multiple
values can be returned.
o If you want variables of different types to be returned, then the var
statement must be placed in front of each of them.

Refer back to the AddFive example


• Change the parameter iNumber to a reference parameter by adding the var
statement in front of the parameter in BOTH the declaration and implementation of
the procedure.
procedure TForm1.AddFive ( VAR iNumber : integer ) ;

• Now following the code in the example, iNum is given a value of 51.
• iNum is displayed using a showmessage. (Value of 51 is displayed to user)
• AddFive procedure is called with iNum as a parameter.
o Value in the parameter iNumber is the same as iNum given as an argument
(iNumber has a value of 51)
o The procedure adds 5 onto iNumber (iNumber has a value of 56)
o iNumber is linked to iNum so now whatever changes are made to iNumber
will also be reflected in iNum, so iNum now has a value of 56.
o Procedure ends, return to example code
• iNum is displayed using a showmessage. (Value of 56 is displayed to user)

7
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos

Consider the following procedure declaration example:


procedure GetDetails ( sID : string ; VAR iBirthYear : integer ; VAR sGender : string ) ;

• GetDetails procedure takes in three parameters, a string, an integer and another


string.
• Only iBirthday and sGender are reference parameters as they have the
statement var in front of the variables.
• The procedure is called as follws:
GetDetails ( ‘0111215048789’ , iYear , sGen ) ;
• Any changes made to parameters iBirthYear and sGender inside the procedure
will be reflected in the iYear and sGen variables respectively.
• NOTE: When calling a procedure with reference parameters, the respective
arguments must be variables (e.g. iBirthYear) and not values (e.g. 2001) because
a value needs to be returned into that linked variable.

Arrays as parameters in subprograms


• Arrays can’t be used as parameters in subprograms unless they have been
declared as a new data type,
• New data types are declared under the type section.
General Structure – New Data Type

type
<new data type name> = <data type> ;
Example: arrNumbers = array[ 1..10 ] of integer ;

• Variables can now be declared of that new data type.


Example: arrOne, arrTwo, arrThree : arrNumbers ;
All three of the above arrays can store 10 integers.

• Use the new data type when using the array as a parameter.
Examples:
o function GetAverage ( arrTemp : arrNumbers ) : real ;
o procedure DisplayArray ( sHeading : string ; arrData : arrNumbers ) ;
o procedure GetValues ( var arrList : arrNumbers ) ;

8
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos

Additional Links:
• Youtube video playlist:
https://www.youtube.com/watch?v=4MeiMJoSCJk&list=PLxAS51iVMjv8tU5VcNHvfjyJ94LR47sin
• Google drive resource activities:
https://tinyurl.com/MLE-G11IT-Subprograms

youtube.com/user/MrLongEducation

You might also like