Pascal - Passing Pointers to Subprograms

Advertisements


Pointer variables may be passed as parameters in function and procedure arguments. Pointer variables can be passed on both as value and variable parameters; however, when passed as variable parameters, the subprogram might inadvertently alter the value of the pointer which will lead to strange results.

The following program illustrates passing pointer to a function:

program exPointertoFunctions;
type
   iptr = ^integer;
var
   i: integer;
   ptr: iptr;
function getNumber(p: iptr): integer;
var
   num: integer;
begin
   num:=100;
   p:= @num;
   getNumber:=p^;
   end; 
begin
   i := getNumber(ptr);
   writeln(' Here the pointer brings the value ', i);
end.

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

Here the pointer brings the value: 100


Advertisements
Advertisements