A r t i c l e s
Const vs Var Function Parameters
const means you can't write to the const parameter in the body of the function. Beginners have trouble understanding CONST parameters since it could imply that you can only send a constant into the function and not a variable. That is not the case. Just think of the "const" as a read only parameter DURING the the function.
procedure tester(const test: string);
if test = 'ya' then
myvar: string; //this is a var
myvar:= 'blah blah'; //even though this is a var...
tester(myvar); //you can still send it to the procedure, which will turn it
//into a read only constant for you now.
readln; //pause program
var means you can write to the var parameter, unlike the const parameter. This means you can modify your var in the function body. You can write and modify the var outside of the function too, as it goes IN and OUT of the function. A VAR parameter is like a INOUT parameter.
When using a DLL/DSO in pascal:
Using const parameters that are "read only" inside the DLL is a very bood practice if you are concerned about memory management issues.
FooBar(const input: string);
Think of var parameters like "write to" parameters that you are going to pass.
FooBar(var input: string);
If you don't explicitely state what your parameter is, i.e.
Then you cannot be SURE what your parameter is.
To be clear in your code, it is better to explicitely state what your parameter is when you can. Some people just skip the var and const - but I prefer being explicit and stating that I will not write to the string. Sometiems I forget to use const, but when going over code a second time, I try to put it in. The only issue with the const parameters is they make the declarations quite long:
function test (const s: string; const s2: string); stdcall; // much longer
function test (s: string; s2: string); stdcall; //much shorter
If you are an intermediate or beginner, when using pchars and pointers, and var's, and const parameteres, sometimes you will start to get very confused about what is where, and what is a copy, and what is just a pointer.
Since a pchar is pointing to some memory, it will act like a reference even if you do not explicitely declare it as a var parameter. Whereas if you do not explicitely declare other types, they are usually sent in by value (not the original data being sent).
Consider reading and studying about PASSING BY REFERENCE vs PASSING BY VALUE (another confusing topic for beginners or even intermediates. The page below helps shed some light on it:
Passing PChars in Libraries
A var parameter will pass the address of the variable you are working with. i.e. this address is passed, which is pointing to some place in the memory. You are not making a new copy! Const can pass EITHER the content or the address. It depends on the situation. A const can be like a pass by value call, or a pass by reference (but even though by reference, the const (reference) can't be modified, since it is restricted to being read only). Const is usually by reference.. i.e. passing the original address in that points to the data.
subject: pass parameters, pass strings, passing ansistrings, passing strings, pass ansistrings, pass by reference, pass by value, passing pchars, pass pchars, pass a pchar, passing a pchar, by reference vs by value, by value vs by reference, value vs reference, reference vs value.
Note: This Wiki is outdated, personal views may have changed.
This wiki contains info on life, health, humans, nature, programming, database, fads, paradigms, poems, principles, theories.
Articles may contain statements which some may find helpful and encouraging, or even discouraging.
Beware, I believe in the Grand Justice system.