Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Const parameter Posted by TheEvan on 22 Sept 2012 at 6:04 AM
The Const parameter is said to be able to prevent modification of the argument value, but i am able to reassign A as seen below perfectly? So what's the difference between a Const parameter and the default parameter? (other than it working as pointer and hence saving memory for large arguments)
procedure Example(Const A: Integer);
begin
  A:=5;
end;   

Report
Re: Const parameter Posted by quikcarlx on 22 Sept 2012 at 6:56 PM
The Const declaration is used for a variable that needs definition but you know will not change (like Pi). Const is not used in declaring variables for a procedure or function. However, var can be used when you have an array or you want to pass
values through a variable. This should be done by the compiler with a pointer. You can pass a constant to a function or procedure but you can't pass anything back since the compiler should catch this mistake. The var before a variable should fix the problem.
Report
Re: Const parameter Posted by TheEvan on 23 Sept 2012 at 6:15 AM
I'm afraid you are not answering my qns at all, i already know everything you mentioned. Const can be used to in front of a parameter as well (as seen in my example)
http://www.freepascal.org/docs-html/ref/refsu58.html#x156-16600014.4.4

The question is, under what situation adding a Const (instead of leaving it undefined) in front of the parameter will change the output result (or make the code in-executable)?
Report
Re: Const parameter Posted by TheEvan on 23 Sept 2012 at 6:17 AM
-duplicate post due to lagging-
Report
Re: Const parameter Posted by TheEvan on 23 Sept 2012 at 6:19 AM
-duplicate post due to lagging-
Report
Re: Const parameter Posted by quikcarlx on 23 Sept 2012 at 6:01 PM
I read the material that your message-link provided and it really gets abstract. So I'll put this simply; would you want to repair a program with that kind of stuff and no explanation of why it is being done or why it has to be that way? I opt for the simple and easily documented so that I can understand it in the future and other programmers can also. Nothing like cryptic code with a cryptic explanation that leaves you wondering what the programmer was thinking. You don't know how many times I've looked at other people's FORTRAN programs, and wondered what he/she was thinking when there are easier ways to get the results that you're after.
Please stay away from the unusual unless you can eloquently explain what you're trying to do.
Report
Re: Const parameter Posted by TheEvan on 24 Sept 2012 at 2:41 AM
What is the 'unusual' you are referring to?
It's a pretty simple qns: what is the difference between a Const parameter and a value parameter (i.e. nothing in front of the parameter), other than memory management issues?
The reply here (http://stackoverflow.com/questions/2627166/difference-between-const-reference-and-normal-parameter) seems to imply that that is the only difference.
Report
Re: Const parameter Posted by Actor21 on 24 Sept 2012 at 2:37 PM
The example you posted should not compile.

If your compiler compiles this then my guess is that it simply ignores const in this context, i.e., treats it as a comment. The compiler writers are creating the illusion that their compiler supports const parameters when it actually does not. Try compiling your program with and without the const and see if the two executables differ in size, particularly the size of the data segment.


Report
Re: Const parameter Posted by TheEvan on 25 Sept 2012 at 2:40 AM
How do i check the size in which it executes?
The compiler is quite established, so could it be that it still utilize referencing when Const is added, but just do not attempt to warn user when the Const parameter is reassigned?
Report
Re: Const parameter Posted by Actor21 on 26 Sept 2012 at 3:09 PM

: How do i check the size in which it executes?
: The compiler is quite established, so could it be that it still
: utilize referencing when Const is added, but just do not attempt to
: warn user when the Const parameter is reassigned?
:

I've looked at the link you posted and it appears that my guess as to how your compiler works is incorrect. It says

"no assumptions should be made about how const parameters are passed to the underlying routine. In particular, the assumption that parameters with large size are passed by reference is not correct."

and

"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."

This latter statement is the converse of how Turbo Pascal works and I think it is also contrary to the standard. Apparently using const parameters amounts to promising the compiler that you will not chance the value, allowing the compiler to make certain optimizations. My guess is that if you do not keep that promise you may be creating a bug.

If const parameters behave this way in Free Pascal then I think they are to be avoided. My expectation when using const parameters is that they are passed by reference and the compiler will not let you make assignments to them. If you really do not have enough memory to pass the parameter by value then better to use a var parameter and include a comment in the code that the value should not be changed.

For what it's worth I think the program in your link is simply bad code.
Var  
   S : String = ’Something’;  
 
      Procedure DoIt(Const T : String);  
      begin  
         S:=’Something else’;  
         Writeln(T);  
      end;  

begin  
   DoIt(S);  
end. 





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.