Wiki


Pascal - Return Pointer from Subprograms

Locked

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 

A function can return a pointer as its result. The following program illustrates returning pointer from a function:

program exPointersFromFunctions;
type
   ptr =^integer;
var
   i: integer;
   iptr: ptr;
function getValue(var num: integer): ptr;
begin
   getValue:=@num;
end;
begin
   i :=100;
   iptr := getValue(i);
   writeln('Value deferenced: ', iptr^);
end.
 

When the above code is compiled and executed, it produces following result:

Value dereferenced: 100