Jump to content

[PASCAL] Functions as input parameters


CouldnoT
 Share

Recommended Posts

Functions as input parameters

 

We can call the function as a procedure/function input parameter, because we can treat it as a value. See the example below:

See the example below:

 

function DoubleNumber(x: Integer): Integer;
begin
 Result:= x * 2;
end;
// Main
begin
 Writeln('The double of 5 is : ', DoubleNumber(5));
 Readln;
end.

 

Note that we have called the DoubleNumber function from inside the Writeln procedure. In the next modification of the example, we will use an intermediate variable to store the function's result, and then use it as input to the Writeln procedure:

 

function DoubleNumber(x: Integer): Integer;
begin
 Result:= x * 2;
end;
// Main
var
 MyNum: Integer;
begin
 MyNum:= DoubleNumber(5);
 Writeln('The double of 5 is : ', MyNum);
 Readln;
end.

 

We can also call functions within if conditions and loop conditions:

 

function DoubleNumber(x: Integer): Integer;
begin
 Result:= x * 2;
end;
// Main
begin
 if DoubleNumber(5) > 10 then
 Writeln('This number is larger than 10')
 else
 Writeln('This number is equal or less than 10');
 Readln;
end.

 

Edited by CouldnoT
Link to comment
Share on other sites

  • CouldnoT changed the title to [PASCAL] Functions as input parameters
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

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.