T7 - Arrays and Subroutines Flashcards
Arrays:
data structure that allows you to hold many items of data which is referenced by one identifier
To find an array length do
variable.len for python
variable.length in pseudocode
arrays and data types stored
All items of data in the array must be of the same data type
arrays works how
Information can be read from or added to the array using the index number
array indexes
Indexes in most languages start at 0 not 1
Arrays lengths
have fixed lengths
Linear search,
- each item is examined in sequence until the item is found or the end of the list is reached
Bubble sort:
- Each item in a list is compared to the one next to it, and if it is greater, they swap places
- At the end of one pass through the list, the largest item is at the end of the list
- this is repeated until the items are sorted
how to loop arrays
By using a FOR loop we can easily loop through an array.
Subroutines:
allow code that you intend to use a number of time to be grouped together under one name
eg of subroutines
Both functions and procedures are subroutines
features of subroutines
Values can be passed to both procedures and functions
functions and outputs
Functions will return a value after processing has taken place
procedures
Procedures - execute code in a subroutine, but they don’t return anything
arguements
Both functions and procedures have arguments = eg (“Hello”) or (1,6)
Parameters:
sum(a, b)
a and b are known as parameters
Pros of subroutines:
- They are useful to break up a large program into self-contained units
- Each subroutine can be tested separately to make sure it works correctly
- Many programmers can work on a large program at the same time, cutting down development time
- can be re-used in other programs
- can be stored in a subroutine library and used in different programs if required
- Program maintenance is easier – if requirements change then just the affected subroutines need to be modified
“scope” of a variable, constant, procedure or function
defines the parts of the program in which it is recognised and can be used
Local variables:
only exist while the subroutine is executing
They are only accessible within the subroutine
Global variables:
are accessible anywhere in the program
global variable in pseudocode =
global score
Global and local constants:
Constants follow the same rules as variables
A constant declared in the main program can be seen and used, but not changed, anywhere else
Pros of local variables:
keep a subroutine self-contained,
it can be used in any program without variable names conflicting with those used in the calling program
python length number of array
variable.len