Vigay.com11
Feb
Search Vigay.com
 
 
Beginners Introduction to BASIC Programming - Lesson 6
Previous | Next

Procedures and Functions
When you start to write longer programs, you may find that you start to duplicate certain lines in your program. For example, you may wish to check that a certain number is between two values, or to prompt the user to enter a list of names and addresses. This is where PROCEDURES can be useful.

A procedure is any collection of program lines that are kept seperate from the main program and can be executed at any point, just by calling the procedure. The best way to explain this is probably via an example. Take a look at the following program.

10 PROCenter
20 PRINT "You entered "A;
30 PROCspace
40 PROCenter
50 PRINT "You entered ";A
60 END
70 DEFPROCenter
80 CLS
90 PRINT "Enter a number between 1 and 10"
100 INPUT A
110 ENDPROC
120 
130 DEFPROCspace
140 PRINT "Press the SPACEBAR to continue"
150 REPEAT UNTIL GET$=" "
160 ENDPROC

Read through it carefully and see if you can work out what will happen when you RUN it.

When the computer comes across PROCenter at line 10, it will quickly scan down the entire program to see if it can find a DEFPROCenter. If it does, it will jump to that point in the program and carry on running it from there. However, the computer remembers the original line (in this case, 10) so that as soon as it gets to an ENDPROC statement (line 110) it will jump back to where it left off in the main program and carry on running from there - line 20 in our example.

Lets summarise: Define the start of the procedure with the statement DEFPROC (short for DEFine PROCedure) followed by a name. This name can be anything you like, which should help identify what the procedure does. For example, DEFPROCinput, DEFPROChelp, DEFPROCinstructions etc.

Follow your DEFPROC line with the lines of code that you want the computer to execute whenever you call the procedure and then, after your lines of code, add the statement ENDPROC to tell the computer where the end of the procedure is.

By looking at the previous example you can see how using procedures can save program lines. For example, we have entered two values, simply by calling PROCenter twice, bu the actual program lines to perform the task we require are only stored once - in the DEFPROCenter definition section.

It also makes the program easier to read and understand. Once you have tested your procedures to make sure they work correctly, they can also cut down on the time taken to write programs, as you can build up a library of tested and working procedures, so that you can copy them into future programs.

In this way, if you encounter any problems or bugs in your program, you can eliminate procedures that you know work.

Functions work in a similar way to procedures, except that they usually perform some sort of mathematical calculation, and return a result to the main program.

You define the start of a function by a DEFFN followed by a suitable name. Again, you can follow it with a number of program lines. However, instead of ending the function with ENDPROC, you end it with =A, where A can be a variable name (either numeric or string), a number, or even a formula.

Instead of calling a function via a PROC statement, you can use it as a mathematical formula, such as SIN or COS. Lets look at another example, to make things easier to understand;

10 INPUT "Enter a number ";A
20 B=FNdouble
30 PRINT "Double your number is ";B
40 END
50 DEFFNdouble
60 A=A*2
70 =A

Notice how B is defined as the answer to the function at line 20.
In this example, the function is returning a normal variable at line 70.
You can, if you like, make the program even simpler, by deleting line 70 altogether and changing line 60 to;

60 =A*2

Using Parameters
When you get used to programming a bit more, you'll soon find how flexible programming can become, with some commands a lot more flexible than they initially look.

To a newcomer, this can seem a bit confusing. However, you just need to remember that sending parameters to a procedure is a bit like defining variables, apart from the fact that they are only used within the procedure or function itself. Look at the following example. It is the same as the previous example, but changed so that we can pass a parameter to the function.

10 INPUT "Enter a number ";A
20 B=FNdouble(A)
30 PRINT "Double your number is ";B
40 END
50 DEFFNdouble(C)
60 =C*2

The first thing you might notice is that we've introduced a new variable, C. This is mainly to clarify the example. Any parameter variables used in functions and procedures are known as LOCAL variables. This means that they are 'local' to the funtion or procedure and independent of similar variables used elsewhere.

If you run the program above and then type PRINT C, the computer will return 0 because C is just local to the function and has been forgotten about by the time the computer returns to the main program. You could in fact have used variable A in the function.
The reason I didn't is because some readers may become confused seeing A used in line 10 and 20, and also used in the function. However, if used in the function, A would actually be a different A, just used for the procedure and forgotten about as soon as you exit from it and return to the main program.

To distinguish between procedure variables and program variables, we call variables used throughout the entire program GLOBAL variables. Those used a function and procedure parameters LOCAL variables.
Lets try to clarify this a bit by looking at another example.
Read through the following example program and see if you can work out which variables are local and which are global.

10 REPEAT
20   INPUT "Enter a number ";A
30   PRINT "The factorial of ";A;" is ";FNfactorial(A)
40   PRINT "Do you want to run the program again?"
50   key$=GET$
60 UNTIL key$="N" OR key$="NO"
70 END
80 DEFFNfactorial(B)
90 LOCAL loop,total
100 total=1
110 FOR loop=1 TO B
120   total=total*loop
130 NEXT loop
140 =total

The program will display the factorial of a number that you type in (line 20). The variable A is global as it is not inside the procedure. The same is true of variable key$.

However, the variable B is a function parameter and therefore local. We have also created two new variables (loop and total) inside the procedure. As these are not parameters received by the function, and we are only going to use them within the function itself, we insert line 90, which tells the computer to make these two variables local as well. This means that as soon as the function exits (at line 140), loop and total will be forgotten about. This means that if we wanted, we could use variables with the same names in other parts of the program without the computer getting confused.

RUN the program the and type PRINT loop, total and see what results you get. The delete line 90 and re-RUN the program and PRINT loop, total

See how the results differ depending upon whether the variables are local or global.

By now you should be fairly familiar with the use of functions and procedures. Some of the ideas explained in this lesson can be a bit confusing though, so if you're still a bit unsure, try going back and reading the lesson again.

Continue to lesson 7.

Previous | Next


Email Email this page to a friend

Last edit: 11th Dec 2009 at 12:49pm
(792 days ago)

Bookmark with:What are these?
delicious Deliciousdigg Diggreddit redditfacebook Facebookstumbleupon StumbleUpon

RSS Feed

^
 
Valid HTML 4.01!
Valid CSS!
Best viewed with a cup of tea Crafted by RISC OS