using const

General FreeBASIC programming questions.
dani.user
Posts: 284
Joined: Sep 30, 2006 10:41

using const

Postby dani.user » Nov 29, 2007 20:55

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?
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Nov 29, 2007 21:31

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
integer
Posts: 391
Joined: Feb 01, 2007 16:54
Location: usa

Postby integer » Nov 29, 2007 21:55

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?
dani.user
Posts: 284
Joined: Sep 30, 2006 10:41

Postby dani.user » Nov 29, 2007 21:59

integer wrote: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)


This is what I was talking about.

Thank you for your answers.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Nov 29, 2007 22:13

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
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Postby VirusScanner » Nov 30, 2007 0:21

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.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Nov 30, 2007 16:37

@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
Posts: 284
Joined: Sep 30, 2006 10:41

Postby dani.user » Nov 30, 2007 19:59

Thank you notthecheatr
Things are now much more clear after reading the tutorial :)
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Nov 30, 2007 20:54

I like to help people learn :)
integer
Posts: 391
Joined: Feb 01, 2007 16:54
Location: usa

Postby integer » Dec 01, 2007 4:49

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.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Dec 02, 2007 0:10

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.

Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests