Solved

an easy one

Posted on 2000-03-02
10
153 Views
Last Modified: 2010-04-04
hi all,

can one or more explain the meanings of following parameter-declarations and the effect for the calling procedure/function and the procedure/function itself

procedure P(var X : ATyp);

procedure P(const X : ATyp);

procedure P(X : ATyp);

not that i don't know it,
but had at last time some
discussions about that
and maybe someone has a better
explaination than i

meikl
0
Comment
Question by:kretzschmar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 2575732
Hi Meikl,

"an easy one" is probably not a good question title but as it is indeed easy I'll try my luck here :-)

Generally, you have the three choices given here to pass parameters to a function/procedure, but these ways don't describe the location of the parameter but the access mode. This means:

1) var parameters are passed via their reference (memory location) and can be altered
2) const parameters are directly passed values or references which cannot be altered
3) other parameters are copies of the original data put on the stack of the called function, they can be changed but all changes are lost on exit of the function

The consequences for the three ways are:

1) always a pointer is passed independent of the type of the parameter
2) parameters are passed via locations where they fit:
  - up to 32 bit values, including pointers and Int64 (if it can be split) (first 3 parameters only) are passed via EAX, EDX, ECX in that order
  - single, double and extended are passed via FPU stack
  - all other values are passed via the stack
3) like 2) but the compiler allows to use assignments on the values

I hope I forgot not too much :-)

Ciao, Mike
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2575758
So tell me mike on what page of your new book on programming delphi will that appear?

Rob;-)
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2575804
Hey Rob, long time no speak from you. I hope you are doing well!

Well, my preferences lie not in writing books or articles (I just canceled to write an OpenGL article for the german computer magazin c't) but to put my knowledge into superb software (and if possible, free software) :-)

Ciao, Mike
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:RBertora
ID: 2575833
Yes, I was just kidding about the book, jsut my way of saying that was a superbly well put comment.
Happy to be back, (not with a vengance though) just browsing about these days.

C ya

Rob;-)
0
 
LVL 27

Author Comment

by:kretzschmar
ID: 2575837
wow,
looks good, mike
(also learned something,
didn't know the exact registers)

well, i will left this q one day to hear other explaination from other experts.

to the other experts,
be free, to comment the explaination in your words. if there are more than one good explaination (as now the one) i will offer seperate q(s) for this expert(s).

meikl
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2576654
Hmmm... I thought using "const" would mean the same as "var", except that the Delphi compiler doesn't accept assignments/changes of this parameter?

What about this code (didn't test it):

type TBuffer = array [0..9999] of integer;

procedure HackBuffer(const buf: TBuffer);
begin
  buf[0] := 0;  // doesn't work
  PInt(@buf)^ := 0;  // works, is this change lost on exit? I'm not sure...
end;

This one should be added:

procedure P(out X : ATyp);

Which is the same as "var", but Delphi can give out more warnings, like "X may not be defined", if X is not set in P.

Even more interesting are parameters like these, I think:

procedure P(X: array of integer);
procedure P(var X: array of integer);
procedure P(const X: array of integer);

Regards, Madshi.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2576769
Hallo Madshi, ich hab' schon auf dich gewartet :-)

Well, you are right, you can actually modify the buffer this way and the value is really stored in the buffer. But this doesn't conflict with my description. It is passed as reference and the compiler prevents you to change it directly.

The "out" parameter is a good point as it is useful specifically for interfaces where the parameter itself does not pass in a value but only pass out (and any valid interface passed into an out parameter is referenced down).

To your last parameters. Look at this code straight out of my Geometry unit:

function MakeAffineVector(V: array of Single): TAffineVector; assembler;

// creates a vector from given values
// EAX contains address of V
// ECX contains address to result vector
// EDX contains highest index of V

asm
              PUSH EDI
              PUSH ESI
              MOV EDI, ECX
              MOV ESI, EAX
              MOV ECX, EDX
              INC ECX
              CMP ECX, 3
              JB  @@1
              MOV ECX, 3
@@1:          REP MOVSD                     // copy given values
              MOV ECX, 2
              SUB ECX, EDX                   // determine missing entries
              JS  @@Finish
              XOR EAX, EAX
              REP STOSD                     // set remaining fields to 0
@@Finish:     POP ESI
              POP EDI
end;


You can see here that I have not mentioned all possible kind of parameters. Here the "single" values are passed just like any ordinary array (which could well be dynamically created on the stack by the compiler).

Ciao, Mike
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2579192
Hi Mike!

>> Well, you are right, you can actually modify the buffer this way and the value is really stored in the buffer. But this doesn't conflict with my description.

Oh yes, you're right of course...   :-)   I read your comment again and now it strikes my eyes...

And thank you for your array parameter description, will be useful someday...   (-:   I didn't know that before.

Regards, Madshi.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2579290
The most interesting thing for me was the fact that the result vector is also passed via a register (in the function MakeAffineVector). But this depends on the number and kind of parameters. Open arrays always need a hidden parameter telling the upper bound of the array and if the function is part of a class then the so often mentioned implicit "self" parameter also influences the parameter location. In this case all parameters slide "down" one register. Self is passed in EAX, the next parameters in EDX and ECX. All other registers should always be considered as being unitialized and reserved (on exit the same values must be there as were on enter, the direction flag is btw. cleared on enter and must be cleared on exit, otherwise the application comes in serious trouble because the compiler expects it so!). Btw: Usually Delphi holds the reference of a class usually in EDI or ESI and loads it into EAX when calling a method (but this is not guaranteed, of course) but it describes why an application often so badly crashes when registers used in a method aren't restored properly.

Ciao, Mike
0
 
LVL 27

Author Comment

by:kretzschmar
ID: 2579556
well, closing the thread now.

thanks for the comments

madshi i post a separate q for you
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 357
Delphi 2 90
Working with hours 3 87
How to add columns to a RAD Studio FMX grid at design time 1 58
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question