gosub inside If ... endif

Just starting out? Need help? Post your questions and find answers here.
marc_256
Enthusiast
Enthusiast
Posts: 744
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

gosub inside If ... endif

Post by marc_256 »

Hi,

I'm using this simplified code

Code: Select all

Value = 2
  Select Value
    Case 1
      Debug "Value = 1"
      Gosub DrawValue1
    Case 2 
      Debug "Value = 2"
      Gosub DrawValue2
    Case 20 
      Debug "Value = 20"
      Gosub DrawValue20
    Default
      Debug "I don't know"
  EndSelect
this seems not to work,
so, I tried this one ...

Code: Select all

Value = 2
  If Value = 1
    Gosub DrawValue1
  ElseIf Value = 2
    Gosub DrawValue2
  ElseIf Value = 3
    Gosub DrawValue3
  Else
    Debug "I don't know"
  EndIf    

  End


DrawValue1:
    Debug "Value = 1"
    Return

DrawValue2:
    Debug "Value = 2"
    Return

DrawValue3:
    Debug "Value = 3"
    Return
same problem, PB is blocking.

- WIN 8.1 x64
tested with
- PB 5.45 LTS x64
- PB 5.61 Final x64

thanks,
marc,
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
Demivec
Addict
Addict
Posts: 4090
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: gosub inside If ... endif

Post by Demivec »

Both codes work fine for me with no errors or faults.

- WIN 8.1 x64
tested with
- PB 5.45 LTS x64
- PB 5.61 Final x64

I used this modifed version of the incomplete code sample containing the Select/Case method:

Code: Select all

Value = 2
  Select Value
    Case 1
      Debug "Value = 1"
      Gosub DrawValue1
    Case 2 
      Debug "Value = 2"
      Gosub DrawValue2
    Case 3 
      Debug "Value = 3"
      Gosub DrawValue3
    Default
      Debug "I don't know"
  EndSelect
 
  End
  
  DrawValue1:
    Debug "Value = 1"
    Return

DrawValue2:
    Debug "Value = 2"
    Return

DrawValue3:
    Debug "Value = 3"
    Return
marc_256
Enthusiast
Enthusiast
Posts: 744
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: gosub inside If ... endif

Post by marc_256 »

Hi Demivec,

thanks for testing,
it seems that my (very big) program have some bugs ?
It works fine for 10 loops in the If ... end if loop,
and then PB stops.

strange, probably one of my bugs ... :oops:

Is it allowed to use gosub inside select ...endselect and if ... endif loops ?

marc,
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
Bisonte
Addict
Addict
Posts: 1232
Joined: Tue Oct 09, 2007 2:15 am

Re: gosub inside If ... endif

Post by Bisonte »

Another approach, to "simulate" the OnGoto and OnGosub commands of older basic's...
Without stack problems...

Code: Select all

Prototype proto_none() ; Declare your Procedure with Parameters

Structure struct_callprocs ; set it into a structure
  Go.proto_none  
EndStructure

; your procedures
Procedure DrawValue1() 
  Debug "Value = 1"
EndProcedure
Procedure DrawValue2()
  Debug "Value = 2"
EndProcedure
Procedure DrawValue3()
  Debug "Value = 3"
EndProcedure

; Declare an array
Global Dim DrawValues.struct_callprocs(3)

; Set the adresses of the procedures to the array
DrawValues(1)\Go = @DrawValue1()
DrawValues(2)\Go = @DrawValue2()
DrawValues(3)\Go = @DrawValue3()

; Let the show begin ....
Value = 1

If Value > 0 And Value <= ArraySize(DrawValues())
  DrawValues(Value)\Go()
Else
  Debug "Don't know"
EndIf
Thanx to Stargate, who shows me this way years ago.
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: gosub inside If ... endif

Post by cas »

Rewrite your code to use procedures and macros instead of goto/gosub. There is nothing that you can not do with procedures and macros but can with goto/gosub. I can not think of any goto/gosub advantage. Ok, maybe code readability in some cases. But you can always name your procedure or macro gosub_DrawValue() in these rare cases.
marc_256
Enthusiast
Enthusiast
Posts: 744
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: gosub inside If ... endif

Post by marc_256 »

Hi Bisonte,
thanks for your help,
I rewrote my program with goto's in place of gosub's
and all works well,
so, it seems you are right it seems to be a problem with gosub return address. (? stack)

Hi cas,
well I'm programming for a long time this way,
and never had problems till this morning.
I must say that I just upgraded PB to PB 5.45F LTS x64 and 5.61F x64

to all,
- is there someone who knows how deep the gosub nesting may be in PB (levels)
- how big is the stack used by PB ?
- and can I set this stack memory size myself ?

tanks,
marc
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
marc_256
Enthusiast
Enthusiast
Posts: 744
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: gosub inside If ... endif

Post by marc_256 »

I think I found some errors in my programming,

http://www.purebasic.com/documentation/ ... thers.html
and
http://www.purebasic.com/documentation/ ... tinue.html

I gone a read some stuff about BREAK and CONTINUE first :oops:

marc,
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
Post Reply