Студопедия

Главная страница Случайная страница

Разделы сайта

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






Global and Local Variables in Functions






By default, values used inside functions are treated as local, which means that if any variables are changed inside a function, they will NOT be changed throughout the rest of your program:

x = 10 fn.def changex(x) x = x + x print " X = " + xfn.end changex(q) % prints 'X = 20' print " Outside the function, X is still: " + x % still 10

You can change this default behavior, and specify that any parameter value be treated as global (CHANGED throughout the rest of your program), by using " & " symbol on the global parameter. This is referred to as " passing the variable by reference":

x = 10 changex(& x) % inside the function, x is now 20, and it % has been CHANGED outside the function too print " Outside the function, X is now changed to: " + x

In most cases, to obtain a value computed within a function definition, you'll simply return a value, instead of passing global variables. For example, it's much easier to just return a computed value from the above function, and use the returned value elsewhere in your program:

x = 10 fn.def addedval(x) y = x + xfn.end y = addedval(x)print y

7. Data Structures: Arrays, Lists, Bundles, and Stacks

In RFO Basic, multiple pieces of grouped data items are stored in " arrays", " lists", " bundles", and " stacks". These data structures are basically used to store lists of data. Lists of data are tremendously important in virtually all types of app programming.

Arrays

Arrays are most typically used to store lists of info which don't change in length. To create an array in RFO Basic, use the " array.load" function. The parameters of the array.load function are a list of strings or numbers, separated by commas - those two types of data can NOT be mixed in arrays (an array must contain either all numbers, or all text items). The first item in the parameter list is the variable label chosen to represent the items in the list:

array.load nums[], 1, 3, 5, 2+5, 9

You can select items from the array using a numerical " index". Indexes in RFO Basic are " one based", meaning the item count starts with 1 (many programming languages are zero based):

print nums[1] % prints 1 - the first item in the arrayprint nums[2] % prints 3 - the second itemprint nums[4] % prints 7 - the fourth item

You can continue written data onto new lines of code using the tilde (~) character:

array.load days$[], " Monday", " Tuesday" ~ " Wednesday", " Thursday" ~ " Friday", " Saturday", " Sunday" print days$[1]print days$[6]

Arrays can only be created (" dimensioned") once during a program. If you need to reload an array with different values, first use the " array.delete arrayname$[]" function, then re-create the array. It's a good idea to get in the habit of using array.delete every time you create an array:

array.delete a$[]array.load a$[], " adf", " qwe"

RFO Basic contains a number of functions to organize and manage data in arrays. Try typing or pasting all these functions into the RFO Basic interpreter, to see how they work:

array.reverse nums[] print nums[] array.shuffle nums[]print nums[] array.sort nums[]print nums[] array.copy nums[], newnums[]print newnums [] array.length lngth, nums[] print lngth array.sum s, nums[] print s array.average a, nums[] print a array.min m, nums[]print m array.max x, nums[]print x array.variance v, nums[] print v array.std_dev sd, nums[]print sd array.delete nums[]print nums[]

Multidimentional arrays allow for lists of lists:

dim l, [1, 2, 3], [11, 12, 13], [21, 22, 23]print l[1][1]print l[2][1]print l[3][2]

Multidimentional arrays are useful for storing matrices of coordinate locations in graphic applications, and other situations in which groups of related lists of info need to be placed into a larger single list.

IMPORTANT: The size of an array cannot be changed once it is created. If you need to add a new item to an array, beyond the array's original size, you must first make a copy of it, then delete the orginal array, then create a new array of the desired size, and then populate it with data from the copied array, and then add the new item(s) to it. Arrays are designed to store lists of info that don't change, such as the list of months in a year, the top ten high scores in a game, the locations of a given number of unchanging images in a graphic layout, etc.

Lists

Lists work much like arrays, but their size can be changed as needed. Lists are designed to hold and manage user data, such as inventory items in a retail store, the names of friends in a contact database, the list of files in a directory, etc. Like arrays, lists can only contain a single type of data. All the data in any list must be either all strings (text), or all numbers. If you need to store mixed data types, store each element as a string, and then use the val() function to convert numerical values.

Lists are created using the " list.create" function. The first parameter tells whether the list will contain text (" string") or number items. The second parameter is a label for the list (a numerical " pointer" variable):

list.create s, myfriendslist.create n, mynumbers

The " list.add" function adds items to the list. The first parameter is the label (as entered in the list.create function above). Notice that the tilde symbol (~) is used to continue items onto additional lines of code:

list.add myfriends, " John", " Bob", " Mike", " Joe" ~ " Sue", " Bill", " Amanda", " Jim", " George", " Tim"

RFO Basic has a number of functions that allow you to organize and manage data in lists. Try these examples in the RFO Basic interpreter:

list.size myfriends, sprint s list.type myfriends, t$print t$ list.insert myfriends, 2, " Steve" list.size myfriends, sprint s list.remove myfriends, 3list.size myfriends, sprint s list.get myfriends, 3, s$print s$ list.replace myfriends, 5, " Paul" list.search myfriends, " Paul", cprint c list.add.array myfriends, days$[]list.size myfriends, sprint s list.create s, newfriendslist.add newfriends, " Tristan", " Peter" list.add.list myfriends, newfriendslist.size myfriends, sprint s list.ToArray myfriends, myfriends$[]print myfriends$[7] list.clear myfriendslist.size myfriends, sprint s

Lists are the data storage workhorse of RFO basic. You'll use them regulary to manage all types of data collections in your programs.






© 2023 :: MyLektsii.ru :: Мои Лекции
Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав.
Копирование текстов разрешено только с указанием индексируемой ссылки на источник.