#DEFINE constant number

techElder

Well-known member
I'd be interested in what you think are the PROs and CONs of using a compiler DIRECTIVE to establish constant values as opposed to using a PROGRAM constant.

For example: #define PORTADIRS %11111100 vs. symbol PORTADIRS = %11111100

In program: dirsA = PORTADIRS
 

hippy

Technical Support
Staff member
In this case both are equivalent so I can see no real advantage or disadvantage in using one or the other.

A #DEFINE will be expanded when it is used in the main code while a SYMBOL definition will be evaluated where it appears so there can be different outcomes and even errors when the expansion is trying to do something the compiler does not understand. For example, if using SERTXD(#K) the SYMBOL definition below compiles and works as expected but the #DEFINE doesn't -

Symbol K = 1 + 2

#Define K 1 + 2
 

techElder

Well-known member
Procedurally, I'm leaning towards SYMBOL constants (as I've always used), because I'm finding it a bit harder to track down #DEFINE constants in my multi-slot programs. IMHO.

I was wondering if I was missing some advantage to using #DEFINE constant.

Sounds like its 6 one way and a half-dozen the other.
 

Goeytex

Senior Member
For constants I prefer Symbol. I use #define for things like below.

Code:
[color=Navy]#define [/color][color=Black]SerPrint [/color][color=Blue]Serout c.1[/color][color=Black],[/color][color=Blue]T9600_32[/color][color=Black],[/color]

[color=Blue]do
   [/color][color=Black]SerPrint [/color][color=Blue]([/color][color=Red]"Hello"[/color][color=Blue])
loop[/color]
 

inglewoodpete

Senior Member
For constants I prefer Symbol. I use #define for things like below.

Code:
[color=Navy]#define [/color][color=Black]SerPrint [/color][color=Blue]Serout c.1[/color][color=Black],[/color][color=Blue]T9600_32[/color][color=Black],[/color]

[color=Blue]do
   [/color][color=Black]SerPrint [/color][color=Blue]([/color][color=Red]"Hello"[/color][color=Blue])
loop[/color]
Now that is one that I like.
 

jims

Senior Member
For constants I prefer Symbol. I use #define for things like below.

Code:
[color=Navy]#define [/color][color=Black]SerPrint [/color][color=Blue]Serout c.1[/color][color=Black],[/color][color=Blue]T9600_32[/color][color=Black],[/color]

[color=Blue]do
   [/color][color=Black]SerPrint [/color][color=Blue]([/color][color=Red]"Hello"[/color][color=Blue])
loop[/color]
Thank you Goeytex... I have never used the #define. Learning something new and useful every day from you folks on the FORUM. JimS
 

premelec

Senior Member
Looks like a mini macro - however I tried something like above serout example and it wouldn't compile though my editor [series 5 ] liked it ok - where is there more discussion of directives? [I looked in commands above under # and D and didn't see it...]
 

techElder

Well-known member
I believer these are new things in PE6.

I like the "SerPrint" #define because you can test with serout and easily switch to hserout.
 

premelec

Senior Member
OK someday I'll catch up with editor 6 --- I used #define xyzetc in a program and ed 5 didn't dislike that and then choked on using xyzetc as a function... Just more typing from my standpoint I guess...
 

Circuit

Senior Member
For constants I prefer Symbol. I use #define for things like below.

Code:
[color=Navy]#define [/color][color=Black]SerPrint [/color][color=Blue]Serout c.1[/color][color=Black],[/color][color=Blue]T9600_32[/color][color=Black],[/color]

[color=Blue]do
   [/color][color=Black]SerPrint [/color][color=Blue]([/color][color=Red]"Hello"[/color][color=Blue])
loop[/color]
Jeepers, Goey! That really is neat; derivatives of that will save me yards of typing when I am using RF units and OLED displays etc. So simple, so effective...
Goey, you have now done over two and half thousand contributions to this forum and I really must once again say how appreciative I am of your teaching. Isn't it about time that you wrote a book for us? - I copy many of your excellent ideas into a OneNote book so that I can reference them easily, but a fully-structured book from your expertise would be perfect!
 

Hemi345

Senior Member
I use #define at the top of the program and then reference it like below while testing. Lots of sertxd can eat up program space fast and affect execution times, but they're very handy while debugging. When most of the kinks get ironed out, I can easily comment out the "#define verbose" at the top to save program space and the time it takes to download to the PICAXE. That is, until I want to go back and tinker some more, then it's nice to have all this in there to help debug the program again. :)

Code:
#define verbose

b0 = b1 + b2

#ifdef verbose
  sertxd ('b0 = ',#b0,13,10)
#endif

b0 = b1 + b10

#ifdef verbose
  sertxd ('b0 = ',#b0,13,10)
#endif
 

Goeytex

Senior Member
The manuals don't provide a whole lot about the new pre-processor directives, #define and #macro. These are really nice features and can help make programming faster and neater, but they only work in PE6. Information on #define and #macro can be found in the PE6 Release Notes. Everyone using PE6 should download this and read it thoroughly.

What I do is put a Picaxe on a breadboard with a download circuit, and TRY EVERYTHING. I use the simulator for syntax checking and basic sanity checks. But do most debugging with a real Picaxe and the terminal or an LCD display.

I do small sections of code at a time until I understand the command or function that I am trying to learn/use. Then I build a program one section at a time making sure each section works before adding the next.

With #define and #macro you can build libraries of "functions" that you can use over and over again. You can make custom "commands" or even "rename" a command. Don't like "sertxd"? Then "rename" it with a #define. But realize that it might make it harder for others to understand your code.

For larger projects you can use #include and put your #defines and #macros in separate files and then #include the file (or files) when you need to use those functions. Just don't be afraid to try stuff and don't ever give up.

A book? Thanks for the compliment, but I am not quite up to doing a book. My background is in Electronics Eng. and I still consider myself a novice at programming. But maybe one day. Until then, I'll keep hanging around and continue learning from the folks here.
 

techElder

Well-known member
Hemi345, in your example, don't forget about " #undefine ". That directive seems like the simplest way to get the compiler to ignore your "verbose" label.
 
Top