Pascal - Return Pointer from Subprograms

Advertisements


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 the following result:

Value dereferenced: 100


Advertisements
Advertisements