Page 148 - Learn to Program - Basic-256
P. 148

Chapter 10: Functions and Subroutines – Reusing Code.                           Page 132


        Sample Output 59: Game Dice Roller




               In the examples above we have created functions that returned a numeric
               value. Functions may also be created that return a string value. A string
               function, like a variable, has a dollar sign after its name to specify that is
               returns a string.



             1      # repeatstring.kbs
             2      # simple string function – make copies
             3
             4      a = "hi"
             5      b = repeat(a,20)
             6      print a
             7      print b
             8      end
             9
             10     function repeat(word,numberoftimes)
             11             result = ""
             12             for t = 1 to numberoftimes
             13                    result ;= word
             14             next t
             15             return result
             16     end function
        Program 60: Repeating String Function



               hi
               hihihihihihihihihihihihihihihihihihihihi

        Sample Output 60: Repeating String Function


               Observe in the function samples, above, that variables within a function exist
               only within the function. If the same variable name is used in the function it
               DOES NOT change the value outside the function.





                                                       © 2019 James M. Reneau (CC BY-NC-SA 3.0 US)
   143   144   145   146   147   148   149   150   151   152   153