EndPsub
Psub <.PsubName.>[(ParamList)] ... [Return] ... EndPsub
 
Parameters: NONE
Returns: NONE
 

      Psubs (protected sub-routines) can be called from different locations in a program. A Psub is a routine that returns a value after it executes. For example:


I = MyPsub()



     calls MyFunction and assigns the result to I. Psub calls cannot appear on the left side of an assignment statement. Psubs that don't return a value can be used as complete statements. For example,

MyPsub()


     calls the MyPsub routine. Psubs can call themselves recursively.

     A Psub is declared with the Psub keyword, followed by the psub name and a list of optional parameters (seperated my commas). The Psub ends with the EndPsub statement that can be followed by a value or expression that will be returned. You can exit a Psub from anywhere in the code with Return, but you cannot return a value or expression this way.



FACTS:


      * Protected Subroutine declaration keywords (Psub & EndPsub) can not be indented.

      * You can return multiple values from a Psub. To do so, list the variables or arrays after the EndPsub statement.

      * All local variables declared within Protected Subroutines are STATIC, so they retain their value between calls.

      * See the Functions&PSub tutorial for more details about Functions and Psubs.






Mini Tutorial:


      This example demonstrates calling PSUB's.


  
; Call the Psubs
  NoReturnValue()
  
  ExitEarly(1)
  
  ExitEarly(5)
  
; Call sub that returns 3 variables
  MyLife, MyName$, MyFloatValue# = MoreReturnValues()
  
; Display the returned values
  Print MyLife
  Print MyName$
  Print MyFloatValue#
  
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  
  End
  
  
; A Psub that does Not Return a value
Psub NoReturnValue()
  Print "This Psub doesn't return anything"
EndPsub
  
; A Psub that conditionally exits
Psub ExitEarly(AValue)
  If AValue = 5
     Print "I exit with Return"
     Return
  EndIf
  Print "I exit with EndPsub"
EndPsub
  
  
; A Psub that returns three values
Psub MoreReturnValues()
  Life = 42
  Name$ = "Deep Thought"
  SomeFLoat#=     4.321
EndPsub Life, Name$, SomeFloat#
  
  
  




This example would output.

  
  This Psub doesn't Return anything
  I Exit with EndPsub
  I Exit with Return
  42
  Deep Thought
  4.321
  

 
Related Info: EndFunction | Function | Functions&Psub | Gosub | Psub | Return :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com