No announcement yet.

value from a sub program

  • Filter
  • Time
  • Show
Clear All
new posts

  • value from a sub program


    I'm trying to get a value back from a sub program (nme$). I compile the sub program (pbc -cu inputit), which creates inputit.pbu
    I then compile the main program pbc test

    When the main program prints nme$ its always empty.

    What am i doing wrong?

    main program (test):
    100 link$ "inputit.pbu"
    110 declare sub inputit(single, single single)
    120 call inputit(25,10,20)
    130 print nme$
    140 end

    sub program (inputit):
    100 sub inputit(lgth,row,col) public
    110 blah blah blah
    120 nme$="hello world"
    130 end sub

  • #2
    To return a value from a procedure in a PBU unit file, you can either pass the variable as a parameter....
      DECLARE SUB inputIt (single, single, single, String)  
      CALL Inputit (25, 10, 20,nme$) 
      Print nme$
    SUB InputIt (l, r, c, N$) Public 
        N$ =  "hello world"
    .. or make the called procedure a FUNCTION returning that value ...
      DECLARE FUNCTION inputIt (single, single, single) AS STRING 
      Nme$ = Inputit (25, 10, 20) 
      Print nme$
    FUNCTION InputIt (l, r, c) Public AS STRING 
         FUNCTION =  "Hello World"
    .. or use EXTERNAL variables, but that is IMO poor practice so I'm not going to show you that.

    Last edited by Michael Mattias; 26 Feb 2009, 09:17 AM.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]


    • #3
      More power, but ....

      You can also use string arrays as in:

      RDIM% = 75
      DIM X$(-RDIM% TO +RDIM%)

      DECLARE SUB inputIt (single, single, X$())

      Yes! Minus to plus values in a variable length string
      array. Why that? Well rolling the positive content items
      into the negative content section of the allocated memory
      is a wonderful way to interface two complete mixes of what
      will be eventually in the 'normal' X$() collection we think about as programmers, from a dual logic major file
      operations code work.

      Slush the as now data. Romp the new data into the old
      place in memory. Operate for comparative and finalization
      on the compare/contrast between the positive and negative exact logic in the master array, for the final new data!

      But I warn you, if you ever have to think about portability for PowerBasic to C or C++ code which thinks
      about what we know as simple strings - as character arrays, and then go toward variable length such character
      arrays forward into object programming in C/C++, you are in for a lot of head scratching. To say nothing if your
      priceless efficient PB code is using X$(-50 TO +50) type memory management design.

      Mike Luther
      [email protected]


      • #4
        Mike, Mike, Mike..... keeping track of the subscripts or testing if positive or negative is so, so, well, Philistine.....
           REDIM  MyArray (any) 
          ' fill it with all kinds of disparate stuff with "like items" in consecutive element numbers
          ' --------------------------------------------------------   
          ' Pass only subscripts  8  thru 16  to a procedure
         ' ---------------------------------------------------------  
           REDIM WorkArray (8) AT VARPTR(MyArray(8))   ' nine elements starting with subscript 8 
          CALL  InterestedFunction ( WorkArray()) 
        FUNCTION InterestedFunction ( W() AS <datatype>) AS something 
           FOR Z = LBOUND (W,1) TO UBOUND (W,1) 
             ' processing here:elements 8 thru 16 of original array 
        No muss, no fuss, no bother.... PLUS you earn seven (7) panache points! Such a Deal!

        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]