Improve company productivity with a Business Account.Sign Up


an easy one

Posted on 2000-03-02
Medium Priority
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

Question by:kretzschmar
  • 4
  • 2
  • 2
  • +1
LVL 10

Accepted Solution

Lischke earned 200 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

Expert Comment

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

LVL 10

Expert Comment

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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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


Expert Comment

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

LVL 27

Author Comment

ID: 2575837
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).

LVL 20

Expert Comment

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);
  buf[0] := 0;  // doesn't work
  PInt(@buf)^ := 0;  // works, is this change lost on exit? I'm not sure...

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.
LVL 10

Expert Comment

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

              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

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
LVL 20

Expert Comment

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.
LVL 10

Expert Comment

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
LVL 27

Author Comment

ID: 2579556
well, closing the thread now.

thanks for the comments

madshi i post a separate q for you

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…

606 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