using const
using const
Hello.
I don't have too much experience with c/c++ so I am wondering why so many functions I see in c headers request const arguments. Should this become common for fb too?
I don't have too much experience with c/c++ so I am wondering why so many functions I see in c headers request const arguments. Should this become common for fb too?
Yes, it should, imho. Constants (as FB is starting to implement them now) are primarily used as a safety net to depend on. Checking for changes in variables which shouldn't change is done by the compiler, not the programmer. Programming with const-correctness is something everyone should be doing. Woohoo ! :D
ALWAYS us constants when the number has more that 5 digits.
You might always be able to type in the same digits all the time,
but there will be that one case when you typo -- and all may be lost.
Most persons have pi memorized to three digits: 3.14 and some have it out to 3.1415926535, and others more so.
The other and probably best reason to use constants, is that six
months later when you provide maintenance -- you will easily
remember what the name is for. however,
m = 6.02214179e23 * Litres
That is a number that you would instantly recognize (because your high school physics teacher demanded that you memorize it), however, some
of your associates might not recall what it was, and then you would tell them that it is Avogadro's constant. If you used the name instead of the digits, it would be self explanatory.
ALSO, by defining it as a constant, you will not be able to alter the value later in the program -- very important.
FreeBASIC has such a keyword.
Passing an argument as a constant to a function or subroutine makes no sense, as it is a waste of overhead and stack.
declare sub foo( byref dummy as const)
Maybe I missed the point of your question. Do you have an example of which you mean?
You might always be able to type in the same digits all the time,
but there will be that one case when you typo -- and all may be lost.
Most persons have pi memorized to three digits: 3.14 and some have it out to 3.1415926535, and others more so.
The other and probably best reason to use constants, is that six
months later when you provide maintenance -- you will easily
remember what the name is for. however,
m = 6.02214179e23 * Litres
That is a number that you would instantly recognize (because your high school physics teacher demanded that you memorize it), however, some
of your associates might not recall what it was, and then you would tell them that it is Avogadro's constant. If you used the name instead of the digits, it would be self explanatory.
ALSO, by defining it as a constant, you will not be able to alter the value later in the program -- very important.
FreeBASIC has such a keyword.
Passing an argument as a constant to a function or subroutine makes no sense, as it is a waste of overhead and stack.
declare sub foo( byref dummy as const)
Maybe I missed the point of your question. Do you have an example of which you mean?
integer, FB has the syntax CONST id [AS type] = value, which acts similar to #DEFINE id value, or ENUM : id = value.
FB is now starting to support const qualifiers (like C/C++), which can be applied to variables preventing changes to that variable, like,
declare sub f (byref n as const integer)
...
var n = 420
f(n)
which means that the procedure f is not allowed to change the value of the variable n.
Declaring procedure parameters as constant makes lots of sense. Google const-correctness and look here for more info:
http://www.freebasic.net/wiki/wikka.php ... Qualifiers
FB is now starting to support const qualifiers (like C/C++), which can be applied to variables preventing changes to that variable, like,
declare sub f (byref n as const integer)
...
var n = 420
f(n)
which means that the procedure f is not allowed to change the value of the variable n.
Declaring procedure parameters as constant makes lots of sense. Google const-correctness and look here for more info:
http://www.freebasic.net/wiki/wikka.php ... Qualifiers
-
- Posts: 775
- Joined: Jul 01, 2005 18:45
integer: Using const variables/parameters doesn't waste any stack space, or any other memory. What it does do is force you not to modify that variable, which is actually very useful because in large projects sometimes you can do things you didn't want to, and it helps the compiler to catch these problems for you.
-
- Posts: 1759
- Joined: May 23, 2007 21:52
- Location: Cut Bank, MT
- Contact:
@integer: Const variables are not the same as Const. I recommend you read my recent tutorial on it, it's a very useful and important new feature in FreeBasic: http://www.freebasic.net/forum/viewtopic.php?t=9993
@dani.user: This should indeed be done the same way in FreeBasic. Const qualifiers and procs have already been implemented, and it is recommended you use them much since they are important and also because they are very new features and need to be bug-tested by the users. In case you want to better understand them, I'll give you the same link I gave integer. This should help a lot: http://www.freebasic.net/forum/viewtopic.php?t=9993
@dani.user: This should indeed be done the same way in FreeBasic. Const qualifiers and procs have already been implemented, and it is recommended you use them much since they are important and also because they are very new features and need to be bug-tested by the users. In case you want to better understand them, I'll give you the same link I gave integer. This should help a lot: http://www.freebasic.net/forum/viewtopic.php?t=9993
-
- Posts: 1759
- Joined: May 23, 2007 21:52
- Location: Cut Bank, MT
- Contact:
stylin & notthecheatr
My initial thoughts were that if you did not want to
alter the value, sent it byval.
The concept seemed like redundant cretinism.
However, after reading the tutorial and doing some searching,
I have revised my thoughts.
I can see where this would save some grief.
The tutorial is excellent!
I really appreciate the clarification.
My initial thoughts were that if you did not want to
alter the value, sent it byval.
The concept seemed like redundant cretinism.
However, after reading the tutorial and doing some searching,
I have revised my thoughts.
I can see where this would save some grief.
The tutorial is excellent!
I really appreciate the clarification.
-
- Posts: 1759
- Joined: May 23, 2007 21:52
- Location: Cut Bank, MT
- Contact:
Of course one major area where this helps is in OOP, where this is passed ByRef ALWAYS and there's no way to change that. It's really an elegant solution in general. Pointers also - passing those ByVal won't prevent the procedure from modifying what the pointer itself points to, so const qualifiers really help there too.