Procedures question


+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Procedures question1488 days old

  1. #1
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,171
    Thumbs Up
    Received: 96
    Given: 1
    Total Downloaded
    2.91 GB

    0 Not allowed!

    Default Procedures question

    Can I have a Subroutine within a procedure block?
    Something like

    Code:
    Proc MyProc
    Dim MyLocalVar
    Goto OverMySub
    MySub:
     'Do something here with MyLocalVar
    Return
    
    OverMySub:
    ' do something else with MyLocalVar
    EndProc
    JohnB

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,513
    Thumbs Up
    Received: 318
    Given: 152
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Re: Procedures question

    Yes.

    I'm not sure why a subroutine would be required within a procedure, but it is possible. Why not simply create another procedure and call that? Remember, the code within a procedure is not put into the final code unless it is called.

    Also, the name of the subroutine within the procedure will have a local name so that it cannot be seen by any other procedures or global code.

    Subroutines and procedures also have the benefit of quite a large stack on 24-bit devices. The stack can also be increased by the user if more and more procedures or calls are going to be used.
    Last edited by top204; 8th March 2016 at 13:03.

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,171
    Thumbs Up
    Received: 96
    Given: 1
    Total Downloaded
    2.91 GB

    0 Not allowed!

    Default Re: Procedures question

    Originally I did write them as Procedures but as they would only have been called by the one procedure I wondered if it would reduce the overhead as there would not be any parameter passing. I guess if I passed them ByRef it would not require parameter passing anyway.

    I was using it initially in a Bressingham ellipse procedure where the plot routine gets called multiple times.
    JohnB

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,513
    Thumbs Up
    Received: 318
    Given: 152
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default Re: Procedures question

    ByRef is still a parameter. It holds the address of the variable named in the calling procedure.

    I can see your logic in a subroutine within a procedure, and it will work as expected.
    Last edited by top204; 8th March 2016 at 14:45.

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  9. #5
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default Re: Procedures question

    For me, doing as John asked is a more modular approach and makes code easier to understand when revisited later.
    I use this approach in Delphi. Good to know it's OK in P24.
    John D.

    Whoops, misread John's question. I was thinking of Procedures within procedures. Are they OK Les?
    Last edited by John Drew; 8th March 2016 at 21:13. Reason: misread question methinks

  10. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  11. #6
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,171
    Thumbs Up
    Received: 96
    Given: 1
    Total Downloaded
    2.91 GB

    0 Not allowed!

    Default Re: Procedures question

    Why would you want to call a procedure within a procedure? I agree in Delphi it is a very nice way of keeping things together but to me the main reason for using a sub within a procedure is to avoid passing when the variables in question are already within scope. In the code below, I have 2 subroutines which use parameters which I have set up in the main procedure.

    Code:
    Proc Tft_Ellipse(pCX As WREG5, pCY As WREG6, pXRadius As WREG7, pYRadius As WREG8, pFilled As Byte)
      Dim x As Word
      Dim y As Word
      Dim xChange As SDword
      Dim yChange As SDword
      Dim ellipseError As SDword
      Dim twoASquare As SDword
      Dim twoBSquare As SDword
      Dim stoppingX As SDword
      Dim stoppingY As SDword
      Dim ASquare As SDword
      Dim BSquare As SDword
    
    GoTo OverSubs
    
    QPlot:
      GLCD_Plot(pCX + X, pCY + Y)
      GLCD_Plot(pCX - X, pCY + Y)
      GLCD_Plot(pCX - X, pCY - Y)
      GLCD_Plot(pCX + X, pCY - Y)
    Return  
    
    QFill:
      Dim SX As WREG5
      Push pCX            ' Save current value of WREG5
      SX = pCX - X 
      Repeat
        GLCD_Plot(SX, pCY + Y)
        GLCD_Plot(SX, pCY - Y)
        Inc SX
      Until SX > pCX + X
      Pop pCX             ' Restore previous value of WREG
    Return
      
    OverSubs:  
      ASquare = pXRadius * pXRadius
      BSquare = pYRadius * pYRadius
      twoASquare = ASquare << 1
      twoBSquare = ASquare << 1
      
      x = pXRadius
      y = 0
      xChange = BSquare * (1 - (pXRadius << 1))
      yChange = ASquare
      ellipseError = 0
      stoppingX = twoBSquare * pXRadius
      stoppingY = 0
      penPosition = 0
      While stoppingX >= stoppingY
        If pfilled = True
          GoSub QFill
        Else     
          GoSub QPlot
        EndIf
        Inc y
        stoppingY = stoppingY + twoASquare
        ellipseError = ellipseError + yChange
        yChange = yChange + twoASquare
        If (ellipseError << 1) + xChange > 0  Then
          Dec x
          stoppingX = stoppingX + twoBSquare
          ellipseError = ellipseError + xChange
          xChange = xChange + twoBSquare
        End If
      Wend
    
      x = 0
      y = pYRadius
      xChange = BSquare
      yChange = ASquare * (1 - (pYRadius <<1))
      ellipseError = 0
      stoppingX = 0
      stoppingY = twoASquare * pYRadius
      penPosition = 0
      While stoppingX <= stoppingY 
        If pfilled = True
          GoSub QFill
        Else     
          GoSub QPlot
        EndIf
        Inc x
        stoppingX = stoppingX + twoBSquare
        ellipseError = ellipseError + xChange
        xChange = xChange + twoBSquare
        If (ellipseError << 1) + yChange > 0  Then
          Dec y
          stoppingY = stoppingY - twoASquare
          ellipseError = ellipseError +  yChange
          yChange = yChange + twoASquare
        End If
      Wend
    EndProc
    PS not fully tested as I don't have a suitable display card yet.
    Still trying to source a ILI9325 based Shield.
    JohnB

  12. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  13. #7
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default Re: Procedures question

    To be accurate I more often use Functions within a Procedure in Delphi, mostly for ease of reading later and partly a habit resulting from the beautiful structure and clarity of Pascal. My question was really based on curiosity but I never know when I'm going to use a little snippet of information like your question and Les's answer for P24.
    I understand your reason regarding variables within scope, however in P24 they are always in scope, they are available outside the procedure merely by adding the name of the Procedure before the name of the variable, but I'm probably telling you 'how to suck eggs' as they say
    Cheers
    John

  14. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  15. #8
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,171
    Thumbs Up
    Received: 96
    Given: 1
    Total Downloaded
    2.91 GB

    0 Not allowed!

    Default Re: Procedures question

    Really, that is a new one on me. Looking through the manual it explicitly states that local variables cannot be accessed outside their procedure block. I did notice that the IDE identifies Variables in a procedure in the form ProcName.VarName but I assume that is just for clarification in the IDE.

    What other gems am I missing?
    JohnB

  16. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  17. #9
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default Re: Procedures question

    Hmmm, maybe my head's RAM has let me down. I will have to read the book!
    I'm fairly sure that 'local' variables are not destroyed and recreated, and behave as I said - I think I used them in that way in a program. On my tablet at present but will check some code later this morning. Les is probably watching this and muttering why I haven't read the manual.
    John

  18. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  19. #10
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,171
    Thumbs Up
    Received: 96
    Given: 1
    Total Downloaded
    2.91 GB

    0 Not allowed!

    Default Re: Procedures question

    The manual says they are not recycled, i.e. they cannot be reused but it doesn't mean they are accessible. That's my reading of it.
    If its not the case the manual needs clarifying.
    JohnB

  20. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  21. #11
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default Re: Procedures question

    John, I just tried this, it compiles but I don't have a device setup to try it in the real world to check that the variable has been assigned.

    Code:
    Device = 24HJ128GP502
    Declare Xtal = 79.23
    TRISB = 00000000000000
    
    ' Configure for internal 7.37MHz oscillator with PLL
    ' OSC pins are general purpose I/O
    ' 
        Config FBS = BWRP_WRPROTECT_OFF, BSS_NO_FLASH, BSS_NO_BOOT_CODE
        Config FSS = SWRP_WRPROTECT_OFF, SSS_NO_FLASH, RSS_NO_SEC_RAM
        Config FGS = GWRP_OFF, GCP_OFF
        Config FOSCSEL = FNOSC_FRCPLL, IESO_OFF
        Config FOSC = POSCMD_NONE, OSCIOFNC_ON, IOL1WAY_OFF, FCKSM_CSDCMD
        Config FWDT = WDTPOST_PS256, WINDIS_OFF, FWDTEN_OFF
        Config FPOR = FPWRT_PWR128, ALTI2C_OFF
        Config FICD = ICS_PGD1, JTAGEN_OFF
        
    ' Alphanmuneric LCD declares (Ensure these are placed before the Hitachi.inc file is inluded)
    ' Note that the alphanumeric LCD does not use the RW pin, therefore, this must be strapped to Gnd.
    '    
        Declare LCD_DTPin = PORTB.12                ' Port and Pin where the LCD's data pins (D4 to D7) are attached
        Declare LCD_RSPin = PORTB.8                 ' Connects to the LCD's RS pin
        Declare LCD_ENPin = PORTB.9                 ' Connects to the LCD's EN pin  
        Declare LCD_Interface = 4                   ' The interface type to the LCD (4 or 8)
        Declare LCD_Lines = 2                       ' The amount of lines on the LCD (1, 2 or 4)
        Declare LCD_CommandUs = 2000                ' The delay required for commands within the LCD (in us)
        Declare LCD_DataUs = 100                    ' The delay required for data within the LCD (in us)
        Declare Float_Display_Type = Fast
     
    'Declare the stack size - 60 is default  
        Declare Stack_Size = 200  
    
    PLL_Setup(43, 2, 2, $0300)                      ' Configure the Oscillator to operate the device at 79.23MHz 
    
    Dim Answer As Double
    
    GoTo Main
    
    
    Proc EarthMove(),Double                         
    Dim TempVar As Double
          TempVar= 5.6789
          Result = 1.23456
    EndProc
    
    
    
    Main:     
          Answer = EarthMove() * 0.012345          
          Answer = EarthMove.tempvar

  22. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  23. #12
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,171
    Thumbs Up
    Received: 96
    Given: 1
    Total Downloaded
    2.91 GB

    0 Not allowed!

    Default Re: Procedures question

    If that works it could be used as a mechanism for creating structures.
    I.e.
    proc MyStruct
    Dim P1 As Byte
    Dim P2 As String
    EndProc

    MyStruct.P1 = $0F

    I would be very happy to use that, in fact Les could add in a new keyword Struct!
    JohnB

  24. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  25. #13
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default Re: Procedures question

    That may not work because the procedure must be called somewhere in the code or else its code is not included in the compiled program.

  26. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  27. #14
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,171
    Thumbs Up
    Received: 96
    Given: 1
    Total Downloaded
    2.91 GB

    0 Not allowed!

    Default Re: Procedures question

    As a work around you could probably make a set of dummy calls at the start of the program to these procs but the better way would be if Les added a modified Proc type which was always added. I think there may be more to it than this as it is so obvious I can't believe Les hasn't thought about it.
    JohnB

  28. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  29. #15
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default Re: Procedures question

    True, the dummy call was in my sample, at first I left it out and wondered why it wouldn't compile - then I remembered about the need to have the call first. I imagine Les was anxious to avoid unnecessary code when using a library. But it may be possible to have a flag to allow a non-called proc to be added to the code body.
    As it is I find the new structure for procedures in P24 is a real step forward.
    John

  30. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Playing with Procedures
    By AlbertoFS in forum Tips 'n Tricks
    Replies: 20
    Last Post: 19th October 2019, 21:01
  2. Question about Procedures
    By johngb in forum Proton 24
    Replies: 7
    Last Post: 17th March 2017, 12:19
  3. Procedures with Proton
    By ricardourio in forum Proton Plus Compiler v3
    Replies: 4
    Last Post: 3rd January 2015, 13:53
  4. [SOLVED !] functions and procedures
    By marcferr77 in forum Proton Plus Compiler v3
    Replies: 6
    Last Post: 9th April 2008, 10:04
  5. Procedures and Functions
    By the.fox in forum Proton Plus Compiler v3
    Replies: 33
    Last Post: 20th January 2006, 22:27

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts