Board index » delphi » Parameter passing
Michael Bester
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
|
Michael Bester
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Parameter passingI am just starting to learn pascal and I was wondering if someone could Your help would be much appreciated. Thanks |
Mike Copela
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Parameter passingQuote> I am just starting to learn pascal and I was wondering if someone could Both classes are passed to the procedure/function via pointers (the subprogram receives the _address_ of something, not its value), and the key difference is what the pointer points to: - Value parameters pass the address of a _copy_ of the data. - Reference parameters pass the address of the _data_. So, in one case (the Reference parameter), the subprogram is given the address of the program's data, and any change to it by the subprogram alters the real data - sometimes a risky thing to let a subprogram do. If _value_ parameters are used, the subprogram can change the data all it wants, but the original program data is protected (unaltered), because the subprogram is working with a _copy_ of the program's original data...no harm possible. Reference parameters always have the "var" clause added in the subprogram definition; values parameters don't. For example: procedure Ref_Param (var X : integer); Both subprograms will receive a passed parameter (type integer) which |
Osmo Ronkan
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Parameter passingIn article <MPG.f51efa7e2b90dd0989...@news.primenet.com>, QuoteMike Copeland <mrc...@primenet.com> wrote: is passed, not any address. (In case of complex parameters (like arrays) technically a pointer to the actual data is passed and then the initialization part of the procedure copies the data to a local variable but in practice this is mostly equivalent to passing the actual data) To illustrate the difference in simple parameters, lets have some code: procedure Value(x:word); procedure Variable(x:word); var z:word; begin Now lets see the code that is produced: Lets first view the calls: PROGRAM.14: value(z); See, how in case of value parameter the actual value of the variable is Now lets see the actual code: First for the value version: PROGRAM.VALUE: begin See, how this only modifies the parameter on the stack. The ret removes Then the variable parameter version: PROGRAM.VARIABLE: begin Notice how this uses les to load the address to the actual parameter in Variable parameters are called that as the actual parameter has to be a Quote> Pascal generates runtime code for all subprogram calls to set parameter on the stack. Quote> Thus, while Value use less stack space. Also whether it is slower even on arrays is not so clear. The copying is pretty fast compared to constant pointer references that variable parameter causes, especially if one compiles to protected mode. Quote> (However, practice to use the value parameter as a local variable. Quote> Note that all this is controlled through the subprogram's arrays/records (excluding strings) of size of exactly 1, 2 or 4 bytes. One can see the difference in the code I presented. (all strings are passed as reference as the size of the actual and formal parameter can be different if one uses $V-) Osmo |
Dr John Stockto
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Parameter passingJRS: In article <MPG.f51efa7e2b90dd0989...@news.primenet.com> of Mon, Quote<mrc...@primenet.com> wrote: Quote>Both classes are passed to the procedure/function via pointers (the var j, k : byte ; PROGRAM.5: x(j, k) ; The address of x, and the value of y, are pushed. -- |
Mike Copela
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Parameter passingQuote> >> I am just starting to learn pascal and I was wondering if someone could to which I was responding: I believe my reply _explained_the_concept_ adequately, and the fact that Borland implements a variation doesn't change the concept, does it? Perhaps I "overdid" my explanation, but I didn't feel an answer needed to go quite into the extreme your denunciation of my reply does... 8<{{ Quote> To illustrate the difference in simple parameters, lets have some code: convey in my reply? Was my answer patently _wrong_ for his request? I don't think so...8<{{ <Sigh> I really don't feel your ripping of my reply served the NG any substantive good. |
Osmo Ronkan
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Parameter passingIn article <MPG.f52dcdbe7038dd4989...@news.primenet.com>, QuoteMike Copeland <mrc...@primenet.com> wrote: any version pushing the address of simple types. That would be counter productive. Value parameters are called that as the value, not the reference is passed on the stack. - Hide quoted text - - Show quoted text - Quote>does it? Perhaps I "overdid" my explanation, but I Quote> <Sigh> I really don't feel your ripping of my reply served the NG any Osmo |
Frank Peel
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Parameter passingQuoteMike Copeland wrote in message ... No. Trying to make value parameters look like reference parameters or Conceptually, when you have a value parameter, only the value is For example, function total(x:integer):integer; Begin As an _implementation_ issue, BP7 has const parameters, which are function total(const x:integer):integer; Begin However, since the programmer promises to the compiler that the value On the other hand, reference parameters (also called Var parameters) A reference is a variable that occupies the same memory as another Procedure total(var x:integer); Var In this simple example, x is just an integer and total could be a FP |
Osmo Ronkan
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Parameter passingIn article <6ccj80$b5...@news.indigo.ie>, QuoteFrank Peelo <fpe...@portablesolutions.com> wrote: parameters. They are pure input parameters like the in parameters in ADA. The underlying implementation can be either value or reference depending on what is optimal. Because the parameters are const well behaving programs work equally well on both. Ill behaving programs can notice the difference. For example: type Tarr=array[1..4] of byte; var arr:Tarr; Procedure xx(const a:tarr); begin The above code outputs 2 but it one changes the array size to 5 it The const parameters are simply of completely different philosophy than Quote
parameters come from) does not even have reference parameters. Instead they have inout parameters and leave even on them the implementation open. Ada also has output parameters that do not pass anything in. (Of course the nice system has been ruined by requirement that only procedures can use inout and out parameters) Osmo |
Frank Peel
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Parameter passingQuoteOsmo Ronkanen wrote in message <6cefam$...@kruuna.Helsinki.FI>... - value parameters were ones where you get the value and can not modify the caller's variables, and the caller therefore did not have to use a variable for the parameter - reference parameters were ones where the parameter is a variable belonging to someone else which the subprogram can modify (these without explicit reference to how the language is implemented) and - const parameters were an answer to the prayers of anyone (like me) who was using var to avoid putting large variables on the stack when calling a procedure even when there was no intention to modify them (and this is very much an efficiency of implementation issue) Guess I should have learned ADA. Quote> Because the parameters are const well programs since the "pchar modifying merged strings" ri-ra. One introduction to ISO standard Pascal said "A parameter may be I wonder what the original poster wanted - an explanation of what FP |
Osmo Ronkan
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Parameter passingIn article <6ch5db$4e...@news.indigo.ie>, QuoteFrank Peelo <fpe...@portablesolutions.com> wrote: how they are passed, not on what their actual effect is. One could produce a variable parameter even without passing any reference. (This would require a C-style method where caller and not the procedure clears the parameters from the stack. ... Quote
Quote
decided on that? Quote> So I was inclined to consider them one or the other, Osmo |
1. what does var do in the parameter passing
2. Why use parameter passing and not global variables?
3. Problem with ADODataSet parameter passing
4. Parameter passing (C -> Delphi)
5. Parameter passing from SQL Server to Delphi
6. Parameter passing Delphi -> ReportSmith for Detail Reports
7. Parameter passing to othetr EXE file, please help...
8. linked list /parameter passing
10. Delphi2.0 Stored Procedure Parameter passing as LongString