Recent

Author Topic: "const" as a method parameter [SOLVED]  (Read 1301 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
"const" as a method parameter [SOLVED]
« on: May 07, 2021, 01:06:04 pm »
I saw some methods have "const" as parameters, but I know that methods take variables as parameters, so what does that mean or how it works or what is the difference - for example - between the next empty two functions I made to test the acceptance of the compiler to the "const" type as a parameter:

Code: Pascal  [Select][+][-]
  1. function func1(c:char):char;
  2. function func2(const c:char):char;
  3.  
  4.  
  5. function func1(c:char):char;
  6. begin
  7.  
  8.   func1:=c;
  9. end;
  10.  
  11. function func2(const c:char):char;
  12. begin
  13.  
  14.   func2:=c;
  15. end;
  16.    
  17.  
  18.  
  19.  
« Last Edit: May 07, 2021, 04:05:14 pm by pascal111 »

dseligo

  • Sr. Member
  • ****
  • Posts: 277
« Last Edit: May 07, 2021, 01:57:43 pm by dseligo »

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: "const" as a method parameter
« Reply #2 on: May 07, 2021, 02:24:20 pm »
إن لم يستبن الفرق لم يكن هناكـ إختلاف في الأثر

google translate:
If the difference is not identified, then there is no difference in the effect

I didn't get the difference, maybe the problem is my weak English, do you know the difference?


« Last Edit: May 07, 2021, 02:40:45 pm by pascal111 »

Handoko

  • Hero Member
  • *****
  • Posts: 4292
  • My goal: build my own game engine using Lazarus
Re: "const" as a method parameter
« Reply #3 on: May 07, 2021, 02:40:30 pm »
@pasacl111

If you were a BASIC user then you may notice Pascal runs faster. That happens because Pascal generated binaries are optimized.

Const parameter is one of them to used for producing optimized binary. If the compiler finds it, it will perform some necessary optimizations.

String is slow, it involves many processes especially for duplicating the data in memory. You usually will see const are used for string parameters.

1. function isSomethingInTheText(const Something, TheText: string): Boolean;
       vs
2. function isSomethingInTheText(Something, TheText: string): Boolean;

The function #1 is a lot faster than the second one. Because it tells the compiler not to generate the code for duplicating the data in memory.

Now, you may ask. Why the compiler does not automatically make it as a const parameter if the data does not change in the function/procedure. That will be great, unfortunately the compiler isn't so smart.

edit:
Fix some typing error.
« Last Edit: May 07, 2021, 02:55:06 pm by Handoko »

wp

  • Hero Member
  • *****
  • Posts: 8391
Re: "const" as a method parameter
« Reply #4 on: May 07, 2021, 02:46:33 pm »
Besides what Handodo was saying, let me note that the most prominent difference for all kinds of types is that you are not allowed to change the "const" parameter inside the function (*). So, if you need, for example, the next character and call c := succ(c) this is only possible in func1, but not in func2. Therefore, using "const" is a good documentation tool to indicate already in the signature of a function "hey, don't care about this parameter, it will not change."

----
(*) Hmmm, not completely exact: When the parameter is a class you ARE allowed to change properties. Quite confusing...
« Last Edit: May 07, 2021, 02:52:28 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Handoko

  • Hero Member
  • *****
  • Posts: 4292
  • My goal: build my own game engine using Lazarus
Re: "const" as a method parameter
« Reply #5 on: May 07, 2021, 02:48:37 pm »
When the parameter is a class you ARE allowed to change properties. Quite confusing...

Yes, that confusing.

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: "const" as a method parameter
« Reply #6 on: May 07, 2021, 02:50:41 pm »
@pasacl111

If you were a BASIC user then you may notice Pascal runs faster. That happens because Pascal generated binaries are optimized.

Const parameter is one of them to used for producing optimized binary. If the compiler finds it, it will perform some necessary optimizations.

String is slow, it involves many processes especially for duplicating the data in memory. You usually will see const are used for string parameters.

1. function isSomethingInTheText(const Something, TheText: string): Boolean;
       vs
2. function isSomethingInTheText(Something, TheText: string): Boolean;

The function #2 is a lot faster than the first one. Because it tells the compiler not to generate the code for duplicating the data in memory.

Now, you may ask. Why the compiler does not automatically make it as a const parameter if the data does not change in the function/procedure. That will be great, unfortunately the compiler isn't so smart.

الفرق في المستودى الأدنى من المعالجة وليس فرقاً ظاهراً

google translate:
The difference is in the lowest level of processing, not an apparent difference.

I see now, for that I touched no apparent effect if I used any case of my functions.

Handoko

  • Hero Member
  • *****
  • Posts: 4292
  • My goal: build my own game engine using Lazarus
Re: "const" as a method parameter
« Reply #7 on: May 07, 2021, 02:52:37 pm »
In some rare cases, you have to use var parameter. You will know it when you need it.

Oops, my mistake in the previous post. The second function is a lot slower than the first one.
« Last Edit: May 07, 2021, 02:56:38 pm by Handoko »

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: "const" as a method parameter
« Reply #8 on: May 07, 2021, 02:56:53 pm »
In some rare cases, you have to use var parameter. You will know it when you need it.

Oops, my mistake in the previous post. The second function is a lot slower than the first one.

إستخدام var"" مألوف لديّ من تعاملي المُسبق على "turbo pascal" حيث تغيير القيمة يقع على المتغيّر الممرر نفسه إلى الدالة أو الإجراء


google translate:
The use of "var" is familiar to me from my previous argument on "turbo pascal", where the change of the value occurs on the variable itself passed to the function or procedure

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: "const" as a method parameter
« Reply #9 on: May 07, 2021, 02:59:02 pm »
In some rare cases, you have to use var parameter. You will know it when you need it.

Oops, my mistake in the previous post. The second function is a lot slower than the first one.

I noticed that!

Handoko

  • Hero Member
  • *****
  • Posts: 4292
  • My goal: build my own game engine using Lazarus
Re: "const" as a method parameter
« Reply #10 on: May 07, 2021, 03:03:10 pm »
You may think:
For any variable that is not var, we should make it const parameter. So the program will run faster.

No, const is useful for performance if it is used for string data type only.

Now, you already know about const and var parameters, you should use const whenever possible if the data type is string.

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: "const" as a method parameter
« Reply #11 on: May 07, 2021, 03:06:13 pm »
Besides what Handodo was saying, let me note that the most prominent difference for all kinds of types is that you are not allowed to change the "const" parameter inside the function (*). So, if you need, for example, the next character and call c := succ(c) this is only possible in func1, but not in func2. Therefore, using "const" is a good documentation tool to indicate already in the signature of a function "hey, don't care about this parameter, it will not change."

----
(*) Hmmm, not completely exact: When the parameter is a class you ARE allowed to change properties. Quite confusing...

I see that:

لا يُمكن تمرير دالة لبارامتر من نوع الثوابت كما في حالة الدالة الثانية

google translate:
It is not possible to pass a function to a parameter of the type of constants as in the case of the second function

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 7170
  • Debugger - SynEdit - and more
    • wiki
Re: "const" as a method parameter
« Reply #12 on: May 07, 2021, 03:10:30 pm »
About "const" =>
1)  You are not allowed to change it inside the procedure (compiler will give an error)
2)  You are not allowed to change it "outside" the procedure (nested calls) / (will compile, but crash at runtime)

Code: Pascal  [Select][+][-]
  1. var MyGlobal: Ansistring;
  2. procedure Foo(const s: AnsiString);
  3. begin
  4.   // You can not change "s" / the compiler will not compile it
  5.   // You are not allowed to change MyGlobal (if called as "Foo(MyGlobal"), if you do you can get a crash
  6.   // You are not allowed to change MyGlobal[n]
  7. end;
  8.  
  9. Foo(MyGlobal);


"const" does not so much change (high level) behaviour. It is an optimization.

Using "const" correctly, the compiler can create faster code.

As documented, when you use "const" => then you promise to the compiler that you will not change that value (neither direct, nor indirect).
If the compiler notice you break that promise, it will give an error.
But if you break the promise in a way the compiler does not notice, then you get undefined behaviour (potentially crashes)
« Last Edit: May 07, 2021, 03:12:09 pm by Martin_fr »

pascal111

  • Sr. Member
  • ****
  • Posts: 282
  • Un trabajo en equipo para programas serias.
Re: "const" as a method parameter
« Reply #13 on: May 07, 2021, 03:26:21 pm »
About "const" =>
1)  You are not allowed to change it inside the procedure (compiler will give an error)
2)  You are not allowed to change it "outside" the procedure (nested calls) / (will compile, but crash at runtime)

Code: Pascal  [Select][+][-]
  1. var MyGlobal: Ansistring;
  2. procedure Foo(const s: AnsiString);
  3. begin
  4.   // You can not change "s" / the compiler will not compile it
  5.   // You are not allowed to change MyGlobal (if called as "Foo(MyGlobal"), if you do you can get a crash
  6.   // You are not allowed to change MyGlobal[n]
  7. end;
  8.  
  9. Foo(MyGlobal);


"const" does not so much change (high level) behaviour. It is an optimization.

Using "const" correctly, the compiler can create faster code.

As documented, when you use "const" => then you promise to the compiler that you will not change that value (neither direct, nor indirect).
If the compiler notice you break that promise, it will give an error.
But if you break the promise in a way the compiler does not notice, then you get undefined behaviour (potentially crashes)

تحطّم كامن أو باطن ،أظنّ ﻷنّه خرج عن تدراكـ المنقّح للخطأ فنفّذ المُعالج التعليمات الخاطئة حرفيّاً

google translate:
Latent or subsoil shattering (potentially crashes), I suspect because it was beyond the debugger's awareness of the error, so the therapist "processor" carried out the wrong instructions verbatim.

eny

  • Hero Member
  • *****
  • Posts: 1624
Re: "const" as a method parameter
« Reply #14 on: May 07, 2021, 04:36:30 pm »
تحطّم كامن أو باطن ،أظنّ ﻷنّه خرج عن تدراكـ المنقّح للخطأ فنفّذ المُعالج التعليمات الخاطئة حرفيّاً

google translate:
Latent or subsoil shattering (potentially crashes), I suspect because it was beyond the debugger's awareness of the error, so the therapist "processor" carried out the wrong instructions verbatim.
Maybe something for the 'Other languages' section?

Google translate:
ربما شيء ما لقسم "اللغات الأخرى"؟
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

 

TinyPortal © 2005-2018