Pascal - Passing Pointers to Subprograms
- Details
- Category: Chapter 3
- Published: Sunday, 14 April 2013 17:15
- Hits: 16648
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 following result:
Here the pointer brings the value: 100