Const parameter

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);
end; [/code]


  • quikcarlxquikcarlx Hollywood, Fl
    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.
  • 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)

    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)?
  • -duplicate post due to lagging-
  • -duplicate post due to lagging-
  • quikcarlxquikcarlx Hollywood, Fl
    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.
  • 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 ( seems to imply that that is the only difference.
  • 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.

  • 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]
    : 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."


    "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.
    S : String =
Sign In or Register to comment.

Howdy, Stranger!

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