Jump to content

Recrutăm Designeri Audio și Video. Detalii aici

Sign in to follow this  
CouldnoT

[PASCAL] Functions

Recommended Posts

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.

 

Share this post


Link to post
Share on other sites

Professional Gamers League • View topic - cerere helper

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.