INFORMATION TECHNOLOGY: GRADE 11 TERM 2 TOPIC AIMS OF LESSON RESOURCES Week 5: Practical User defined methods: Functions At the end of this lesson you should know what a function is and how to write and call a function Paper based resources Digital resources DBE MTN Textbook; Unit 6.3 Links on the WCED ePortal (Use your school issued textbook for the same content) The e-resources below has the actual URL to the websites INTRODUCTION In Part 1 and Part 2 we explored procedures and procedures with parameters. Recall that a procedure is a method that performs a specific task. Our final method is Functions. A function is a procedure with a return value. PRE-KNOWLEDGE • Procedures with parameters •• Procedures What is a function? • A function is a procedure that returns a value. {declaring a function with no parameters} private function functionName : returnType; {declaring a function with two parameters} private function functionName(variable1 : type l variable2 : type) : returnType; • unlike a procedure, we need to specify the data type that the function will return. {The body of a function with one parameter that returns an integer value} function TForm1.functionName(a : integer) : integer; begin result := someIntegerValue; end; {The body of a function with no parameters that returns a Boolean value} function TForm1.functionName : boolean; begin result := someBooleanValue; end; CONCEPTS AND SKILLS The data type that the function returns may be different from the parameters used. INFORMATION TECHNOLOGY: GRADE 11 • The return value is always indicated with the statement result := … {Calling a function with two integer arguments and a string return type} sVariable := functionName(argument1, argument2); {Calling a function with no parameters and a Boolean return type} if functionName = TRUE then begin // end; {Calling a function with one parameters and a Real return type} sValue := FloatToStr(functionName(argument)); • The data type of the return value of the function must match the data type it is assigned to. • Procedures are standalone statements, whereas functions are always within another statement. Overloading methods • It is possible to use the same method names, granted that o the parameters of each method are different o the overload; modifier is added to the method definition • Example of overloading {method definitions} function A (a : string) : string ; overload; function A (a : integer) : integer; overload; function A : string; overload; procedure B ; overload; procedure B (b : string); overload; • The argument of the method will determine which method definition is used NOTE: Rather than testing the value of the function equal to true with “functionName = TRUE” we can simply use the value of the function as: if functionName then INFORMATION TECHNOLOGY: GRADE 11 ACTIVITIES / ASSESSMENT Passing arrays as an argument (enrichment) NOTE: arrMyArrayType is a new data type, just like INTEGER, • In order to pass an array to a method, we must first declare a new data type REAL, STRING, BOOLEAN. {declaring an array as a data type} TYPE Once a new data type has been sArrType = array[1..3] of string; created, we can use it when we iArrType = array[0..2] of integer; declared variables, e.g. {defining methods with the array data type} function FwithArray ( arrVal : sArrType ) : iArrType; var a : sArrType; procedure PwithArray ( a,b,c : iArrType ; d : sArrType) ; s : string; {calling a method} b : iArrType; pwithArray( myIntArray1, myIntArray2, myIntArray3, myStringArray1); myArray := FwithArray( myStringArray); activities 6.2 (p. 144), activities 6.5 (p. 146 - 147) consolidation activity (p. 152) Exercise 1 For each of the following methods, state whether the method is a function or a procedure. If the method is a function, state the data type that is returned. 1.1 1.3 1.5 1.7 1.9 Edit1.setfocus inc(iCount,3) delete(‘happy’,1,5) showmessage(‘the matrix’) monthof(now) 1.2 inc 1.4 randomrange(0,10) 1.6 copy(‘sad’,1,2) 1.8 tblMyTable.EOF 1.10 RichEdit1.Lines.Add(‘some text’) Exercise 2 State the function definition of each of the following functions 2.1 The function HCF that calculates the highest common factor of two integers. 2.2 The function REVERSE that returns an input string in reverse. 2.3 The function CheckSpaces that determines if a string contains spaces. 2.4 The function CountCharacters that counts the number of specified characters in a given string. 2.5 The function isPrime that checks if a number if prime or not. INFORMATION TECHNOLOGY: GRADE 11 CONSOLIDATION VALUES E-RESOURCES Answers Exercise 1 1.1 1.3 1.5 1.7 1.9 Exercise 3 Write the functions defined in exercise 2. Exercise 4 Use the overload function to define functions called CalculateAverage which can calculate the average of two to four integers. Exercise 5 Use the function isPrime (you wrote earlier) to determine how many prime numbers there are from 1 to 5000. After completing Part 3 you should be able to: • Understand what a function is • Know that a function returns a value • Know how to call a function • Know how to overload a method Procedures and functions are used to make your program more modular and prevent unnecessary duplication of code. Procedures will make your programs more readable and easier to debug. Mr. Longs’ Custom Delphi Functions https://youtu.be/GAQ34Zpa60c Extra Custom Delphi Functions https://youtu.be/hHPyVYkwO_4 Procedure Procedure Procedure Procedure Function, returns an integer 1.2 1.4 1.6 1.8 1.10 Procedure Function, returns an integer Function, returns a string Function, returns a Boolean Procedure Exercise 2 2.1 2.2 2.3 2.4 2.5 function HCF (num1 , num2 : integer) : integer; function REVERSE (s : string) : string; function CheckSpaces (s : string ) : Boolean; function CountCharacters ( c : char ; s :string ) : integer function isPrime (num : integer) : Boolean; OR function CountCharacters ( s : string ; c :char) : integer; INFORMATION TECHNOLOGY: GRADE 11 Exercise 3 {body of function HCF} function TForm1.HCF(num1, num2: integer): integer; var i: integer; iSmallest: integer; iHCF: integer; begin // determining which of num1 or num2 is smallest if num1 < num2 then iSmallest := num1 else iSmallest := num2; for i := 1 to iSmallest do if (num1 mod i = 0) and (num2 mod i = 0) then // factor common to both numbers? iHCF := i; result := iHCF; end; {body of function countCharacters} function TForm1.countCharacters(c: char; s: string): integer; var i, iCount: integer; begin iCount := 0; for i := 1 to length(s) do if c = s[i] then inc(iCount); result := iCount; end; {body of function REVERSE} function TForm1.Reverse(s: string): string; var i: integer; sBackwards: string; begin sBackwards := ''; for i := length(s) downto 1 do sBackwards := sBackwards + s[i]; result := sBackwards; end; {body of function checkSpaces} function TForm1.checkSpaces(s: string): boolean; begin if pos(' ', s) > 0 then result := TRUE else result := FALSE; end; {body of function isPrime} function TForm1.isPrime(num: integer): boolean; var i: integer; begin result := TRUE; // we assume num is prime for i := 2 to num - 1 do begin if num mod i = 0 then result := FALSE; // if we find a divisor, then num is not prime end; end; INFORMATION TECHNOLOGY: GRADE 11 Exercise 4 function CalculateAverage (a,b : integer) : real; overload; function CalculateAverage (a,b,c : integer) : real ; overload; function CalculateAverage (a,b,c, d : integer) : real ; overload; Exercise 5 iCount := 0; FOR i := 1 to 5000 if isPrime(i) then inc(iCount); showmessage( IntToStr(iCount));