CouldnoT Posted July 31, 2020 Share Posted July 31, 2020 Functions Functions are similar to procedures, but have an additional feature, which is returning a value. We have used functions before, like Upcase, which converts and returns text to upper case, and Abs, which returns the absolute value of a number. In the next example, we have written the GetSumm function, which receives two integer values and returns their summation: In the next example, we have written the GetSumm function, which receives two integer values and returns their summation: function GetSumm(x, y: Integer): Integer; begin GetSumm:= x + y; end; var Sum: Integer; begin Sum:= GetSumm(2, 7); Writeln('Summation of 2 + 7 = ', Sum); Write('Press enter key to close'); Readln; end. Notice that we have declared the function as Integer, and we have used the Result keyword to represent the function's return value. In the main application, we have used the variable Sum, in which we receive the function result, but we could eliminate this intermediate variable and call this function inside the Writeln procedure. This is one difference between functions and procedures. We can call functions as input parameters in other procedures or functions, but we can not call procedures as parameters of other functions and procedures: function GetSumm(x, y: Integer): Integer; begin GetSumm:= x + y; end; begin Writeln('Summation of 2 + 7 = ', GetSumm(2, 7)); Write('Press enter key to close'); Readln; end. Link to comment Share on other sites More sharing options...
CouldnoT Posted August 5, 2020 Author Share Posted August 5, 2020 (edited) Edited October 21, 2020 by CouldnoT Link to comment Share on other sites More sharing options...
Recommended Posts