Professional Documents
Culture Documents
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
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
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.
iBiggest := Max3 ( iN1, iN2, iN3 ) ; //Call Max3 function with iN1, iN2 and iN3 as parameters
//and result is returned into the iBiggest integer variable
3
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos
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 ) ;
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”.
5
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos
begin
sTitle := InputBox( ‘’, ‘Enter title or heading:’, ‘’ ) ; //Get heading to be displayed
Explanation:
Procedure AddFive takes in an integer value parameter and adds 5 to it (ONLY IN THE
PROCEDURE).
begin
iNum := 51 ; //Give iNum a value of 51
showmessage( IntToStr( iNum ) ) ; //First Display – display iNum before procedure call
showmessage( IntToStr( iNum ) ) ; //Second Display – display iNum after procedure call
end;
6
Mr Long Grade:
Subject:
11
Information Technology
Version:
Topic:
Beta
Subprograms
Videos
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.
• 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
type
<new data type name> = <data type> ;
Example: arrNumbers = array[ 1..10 ] of integer ;
• 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