Program Layout

Just starting out? Need help? Post your questions and find answers here.
Hot Pockets
User
User
Posts: 51
Joined: Mon Jun 01, 2009 3:56 am

Program Layout

Post by Hot Pockets »

Does anyone have a program layout. Like where do you place the Constants, Declare statements & or procedures.Do all the Declare statements go before the procedures. I wrote a program which shows a BMP file and another having a Menu. When I ran them only the BMP was displayed. If I remarked out the BMP program then the menu would work. Why did I have to remark out 1 of them to get the other to work?
rrpl
Enthusiast
Enthusiast
Posts: 121
Joined: Fri Apr 18, 2008 7:22 am
Location: Australia

Post by rrpl »

You'll find it easier to get help to solve your problem if you post the portion of your code that you are having problems with.

Generally PB can handle things laid out all over the place (I'm guilty!!), but its best generally to declare things up front.

From the PB help file:
Syntax
Declare[.<type>] name(<variable1[.<type>]> [, <variable2[.<type>]>, ...])

Description

Sometimes a procedure need to call another procedure which isn't declared before its definition. This is annoying because the compiler will complain 'Procedure <name> not found'. Declare can help in this particular case by declaring only the header of the procedure. Nevertheless, the Declare and real Procedure declaration must be identical (including the correct type).
Probably a good idea to download this beginners guide from the link underneath to begin with.

http://www.purebasic.fr/english/viewtopic.php?t=37059
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Like a skeleton app you mean?
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I don't use Declare unless it's absolutely necessary. Placing the procedures in the correct order is usually sufficient.

I think it's common to organize the program something like this:
1. Enumerations and constants
2. Structures
3. Global variable declarations
4. Procedures
5. Main program code (or code to call procedure with program code)
6. DataSections
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Trond wrote:I don't use Declare unless it's absolutely necessary. Placing the procedures in the correct order is usually sufficient.
:shock: Why? Is there some problem with having unnecessarily declares?

I always have declares for all procs, helps me structure the code like a little contents section. Also, I've noticed (and it was discussed once) that the position of a proc in the source can effect it's performance surprisingly enough so you may want to take advantage of that.

Is there some benefit to playing with proc order for dependancy purpose?
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

pdwyer wrote:
Trond wrote:I don't use Declare unless it's absolutely necessary. Placing the procedures in the correct order is usually sufficient.
:shock: Why? Is there some problem with having unnecessarily declares?
Yes, they're boring to write.
User avatar
idle
Always Here
Always Here
Posts: 5838
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

what Trond says is right, I can't be arsed declaring statement either but then I'm a lazy git! :lol:
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Placing the procedures in the correct order is usually sufficient

Usually is the right word. Sometimes that's just impossible, when procedures
rely on other procedures and one simply can't go before another.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

aren't you guys part of the pro enable explicit lobby though? :mrgreen:
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

I never use declare either and never had any procedure order issues. :P
--Kale

Image
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

Are we using the same compiler? :shock:
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

PB wrote:> Placing the procedures in the correct order is usually sufficient

Usually is the right word. Sometimes that's just impossible, when procedures
rely on other procedures and one simply can't go before another.
I know, then I use declares. And sometimes the procedure order becomes very illogical without declares, so then I may use declare as well.

Kale, here is a (bad) example where you have to use declare:

Code: Select all

Declare Odd(Num.i)

Procedure Even(Num.i)
  If Num = 0
    ProcedureReturn 1
  Else
    ProcedureReturn Odd(Num-1)
  EndIf
EndProcedure

Procedure Odd(Num.i)
  If Num = 0
    ProcedureReturn 0
  Else
    ProcedureReturn Even(Num-1)
  EndIf
EndProcedure

Debug Even(0)
Debug Even(1)
Debug Even(2)
Debug Even(3)
Debug "--"
Debug Odd(0)
Debug Odd(1)
Debug Odd(2)
Debug Odd(3)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Program Layout

Post by Kaeru Gaman »

Hot Pockets wrote:I wrote a program which shows a BMP file and another having a Menu. When I ran them only the BMP was displayed. If I remarked out the BMP program then the menu would work. Why did I have to remark out 1 of them to get the other to work?
you wrote TWO programs? why?
perhaps you're misinterpreting the nature of procedural programming?

all I can say so far is
Image

for more analysis, we need more information.
oh... and have a nice day.
Hot Pockets
User
User
Posts: 51
Joined: Mon Jun 01, 2009 3:56 am

Program Layout

Post by Hot Pockets »

Kaeru:
Why Indeed? I should of said when I combined them into 1 program only 1 ran.
Trend:
Thanks for answering my questionvery well and to the point. May I have a little more advice. Would it work as well if I reversed the order of items 4 and 5. (Just an idea.)
Others:
I downloaded that most excellent book awhile back and have printed all 336 pages into 2 volumes. I will look thru that again to see if there is something like Trend's accurate synopsis. It may take me several days to do it tho!
ALL:
Thank you for your efforts!
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

Hot Pockets wrote:Kaeru:
Why Indeed? I should of said when I combined them into 1 program only 1 ran.
did you say this?
problem is, when you don't say something, the others can't know something.

I could be guessing around and presume, because you said "two programs"
you also wrote two eventloops instead of one...
sounds like "misinterpreting the nature of procedural programming"

whatever it was, it clearly was "ur doin it wrong"....


the others gave you some good overview answering to your general question...

but it wasn't any solution to your problem "bmp shows, menu doesn't show".
why?
surely because you didn't give the least hint what caused the problem.

also mind this:
rrpl wrote:You'll find it easier to get help to solve your problem if you post the portion of your code that you are having problems with.
oh... and have a nice day.
Post Reply