Skip to main content

5.8) Procedures


So far we have written two functions. The limitation of using functions is that they only return a single variable. Often we would like to return multiple variables. In order to do this, we use what are called "procedures". Procedures are really no different to functions in many ways, except that we use the term "Pro" instead of "Function" in the definition line at the top of the file and the variables listed after the procedure name are both inputs and outputs.

One very common use for a procedure is to read from a file. This is something you will do very often, so you don't want to be reproducing the same bit of code over and over again. So it is best to write a procedure to do it, you can then call it every time you need to read in some data.

For this example we are going to read in data from an ASCII file, which has some header text and then data stored in columns. As our example code is going to start getting larger from this point onwards, we will use comment lines within the code to explain what each line of code does. Type the following into a file called read_ascii_cols.pro.