Recent

Author Topic: Function: To put "const" in arguments  (Read 3168 times)

nana232

  • New Member
  • *
  • Posts: 40
Function: To put "const" in arguments
« on: September 20, 2018, 03:22:09 am »
If I need to declare function ABC.
The function uese the arguments "By Value".
For example:

Code: Pascal  [Select][+][-]
  1. function ABC(x,y:real):real;
  2. Begin
  3.   result:=x*y;
  4. End;
  5.  

How is the advantage to put "const" in front of the arguments like this? :
 
function ABC(const x, y:real):real;

« Last Edit: September 20, 2018, 03:26:22 am by nana232 »
Lazarus 1.8.4 Win10 32bit

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Function: To put "const" in arguments
« Reply #2 on: September 20, 2018, 04:08:34 am »
If I need to declare function ABC.
The function uese the arguments "By Value".
For example:

Code: Pascal  [Select][+][-]
  1. function ABC(x,y:real):real;
  2. Begin
  3.   result:=x*y;
  4. End;
  5.  

How is the advantage to put "const" in front of the arguments like this? :
 
function ABC(const x, y:real):real;
"A function or procedure parameter may be declared const. Any assignment to a const parameter within a procedure or function and the compiler will flag it as an error: "Can't assign values to const variable". Declaring a parameter as const allows the compiler the possibility to do optimizations it couldn't do otherwise, such as passing by reference while retaining the semantics of passing by value. A const parameter cannot be passed to another function or procedure that requires a variable parameter."
http://wiki.freepascal.org/Const

"Specifying a parameter as Constant is giving the compiler a hint that the contents of the parameter will not be changed by the called routine. This allows the compiler to perform optimizations which it could not do otherwise, and also to perform certain checks on the code inside the routine: namely, it can forbid assignments to the parameter. Furthermore a const parameter cannot be passed on to another function that requires a variable parameter: the compiler can check this as well. The main use for this is reducing the stack size, hence improving performance, and still retaining the semantics of passing by value...
Remark: Note that specifying const is a contract between the programmer and the compiler. It is the programmer who tells the compiler that the contents of the const parameter will not be changed when the routine is executed, it is not the compiler who tells the programmer that the parameter will not be changed."
https://www.freepascal.org/docs-html/ref/refsu67.html#x179-20100014.4.4

"const does not guarantee that the value is actually passed by reference.
Free Pascal supports
procedure foo(constref z : integer);
for this purpose: z is always passed by reference in this case. I'am not aware of something similiar in other pascal compiler."
https://stackoverflow.com/questions/25575718/if-a-variable-is-passed-as-a-constant-does-it-get-passed-by-reference

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Function: To put "const" in arguments
« Reply #3 on: September 21, 2018, 12:21:13 am »
The advantage of using a CONST is that the compiler can decide the best way to pass the parameters
to the function in the background. That is, seeing that CONST also denies you from changing its value so
this allows the compiler to configure the parameters by Reference instead of value..

 In the case of a REAL type, depending on the target 32 or 64 bit, the compiler may decide to do it as
a Reference in 32 bit mode since a pointer to the REAL is 4 bytes and fits naturally on the stack, the REAL
is 8 bytes which would require a 2 x push.

  of course I am just assuming I know how FPC behaves on this situation.

 but in any case, a CONST allows FPC a little more freedom and prevents the code to change the source.



 
The only true wisdom is knowing you know nothing

nana232

  • New Member
  • *
  • Posts: 40
Re: Function: To put "const" in arguments
« Reply #4 on: September 21, 2018, 07:05:47 am »
Thanks all  :)
Lazarus 1.8.4 Win10 32bit

 

TinyPortal © 2005-2018