Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Const parameter

TheEvanTheEvan Posts: 7Member
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)
[code]procedure Example(Const A: Integer);
begin
A:=5;
end; [/code]

Comments

  • quikcarlxquikcarlx Hollywood, FlPosts: 26Member
    The [color=Red]Const[/color] declaration is used for a variable that needs definition but you know will not change ([color=Orange]like Pi[/color]). [color=Red]Const[/color] is not used in declaring variables for a procedure or function. However, [color=Green]var[/color] 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 [color=Green]var[/color] before a variable should fix the problem.
  • TheEvanTheEvan Posts: 7Member
    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)?
  • TheEvanTheEvan Posts: 7Member
    -duplicate post due to lagging-
  • TheEvanTheEvan Posts: 7Member
    -duplicate post due to lagging-
  • quikcarlxquikcarlx Hollywood, FlPosts: 26Member
    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.
  • TheEvanTheEvan Posts: 7Member
    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.
  • Actor21Actor21 Posts: 35Member
    The example you posted should not compile.

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


  • TheEvanTheEvan Posts: 7Member
    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?
  • Actor21Actor21 Posts: 35Member
    [blue]
    : 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?
    :
    [/blue]
    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 [b]const[/b] 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 [b]const[/b] parameters behave this way in Free Pascal then I think they are to be avoided. My expectation when using [b]const[/b] 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 [b]var[/b] 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.
    [code]
    Var
    S : String =
Sign In or Register to comment.