- Pascal - Home
- Pascal - Overview
- Pascal - Environment Setup
- Pascal - Program Structure
- Pascal - Basic Syntax
- Pascal - Data Types
- Pascal - Variable Types
- Pascal - Constants
- Pascal - Operators
- Pascal - Decision Making
- Pascal - Loops
- Pascal - Functions
- Pascal - Procedures
- Pascal - Variable Scope
- Pascal - Strings
- Pascal - Booleans
- Pascal - Arrays
- Pascal - Pointers
- Pascal - Records
- Pascal - Variants
- Pascal - Sets
- Pascal - File Handling
- Pascal - Memory
- Pascal - Units
- Pascal - Date & Time
- Pascal - Objects
- Pascal - Classes
Pascal - Passing Pointers to Subprograms
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
pascal_pointers.htm
Advertisements