Arrays Reviewed

  1. Arrays are numbered collections of storage locations.
  2. Arrays are declared like normal variables, with types and names. However we also define their size.
  3. Arrays number their elements from 0.
  4. Array 'indexes' are "integer expressions'. They might be integers, integer variables or
A Common technique, when we don't know how big an array we need is to declare one that is obviously too big and keep track of how many we have actually used. In the handout examine e.g.1. Cut and paste it and see it run.


The last new thing that you need to learn in this course.

When we started programming we mentioned that the program is called the "main function". It sort of follows that there must be funtions that are not 'main'! Why would we make such a thing? Generally the answer is because we have something that we want to do repeatedly in the program and it is neater to write it once ahead of the program. There are also ways of sharing/resusing functions between programs. In fact this is largely what our "include" lines are about.

Void functions

The simplest type of functions is called a "void function". To keep things really simple we will use the following rules.
  1. A functions is written before the functions that calls (uses) it. This means it is above the main function in the file (but after the includes).
  2. It starts with the word "void", followed by its name, followed by a "parameter list", followed by the body of the function -statements enclosed by braces.
  3. The parameter list consists of the "inputs" that the functions needs. It is enclosed in brackets and each "parameter" has a type and the name that we are going to use for it in the function.
  4. When running a function we start at the top of the body and proceed as we did for an ordinary program, until weeither reach the end or we reach the word "return;".
  5. To make use of a function we 'call' it. The main program or another (lower) function calls it by naming it and sending it appropriate parameters.
  6. when we send normal parameters we usually send only copies and changing them in the function doesn't change them in the calling part of the function. This doesn't apply to arrays, however.
In the handout sheet we will look at a simple void function called "bar" that takes a single integer parameter and creates a bar of asterix of the length given by the parameter.

Non Void Functions

As well as receiving data as parameters functions can 'return' a single value to the calling program. Libraries of useful functions exist, so we don't normally have to write out own sin function we just have to link (include) to the correct library. Non void functions are used as parts of expressions e.g. in assignment statements. e.g.
In the handout we have an example of a funtion called mid, that returns the average of two parameters.

Project Ideas

Bubble Sort

The first program on the handout this week does the donkey work for making a bubblesort program in that it enters data into an array, remembers how much data there is and enables you to print out the content. To turn this into bubblesort: a) Create an inner loop where i goes from 1 to less than count (rather than from zero). b) Inside this loop ask (if statement) whther store[i] is less than store[i-1] (and is thus out of order). c) If it is out of order swap them (you need a spare variable): spare=store[i-1]; store[i-1]=store[i];store[i]=spare; d) You then have to put this loop in another which goes through count-1 times (need new counter variable). e) Print out sorted data.

Graphic Functions

Create functions that draw simple designs but whose size, positin etc. is determined by parameters. create eleborate designs by calling them from within loops. Text file handout
Home Page of RDScience