BWD Chapter 09: What's your major function private?
2004-11-14 12:30 - Beginners Web Development
Chapter 09
What's your major function private?
Thanks to chapters seven and eight we have learned a great way to save our fingers and the memory of the computer when we have told it to perform a repetitive task. Furthermore, loops can help eliminate bugs. Imagine if you needed to change the code in a loop. Easy. But if you wrote out the code many times you would have to change each one. Easy to make a mistake.
There is another wonderful tool for repetitive tasks to use when programming. It is called the function. We have learned about a few statements already like print and if. Each statement performs a particular task, when we need to do that tasj, we just use the statement. A function is much the same, but we get to choose what it does.
A regular statement starts off with a word that identifies the statement, then usually some arguments to control exactly what it does. Functions are just the same, but we choose it all rather than using pre set statements.
I can not stress enough how important it is to use functions (and other things) to produce reusable code. This is program code that you can and do use many times. Once you have worked it out perfectly and the whole thing works, there is a complex task you can do in a moment without worrying about mistakes and bugs. This is most important when changes come later on though still helpful from the beginning of a program.
Now for an example!
FUNCTION add (num1, num2) PRINT num1 + num2 END FUNCTION
This is a trivial function that just takes two numbers, adds them together, and displays the result on screen. We could of corse do more complex math in a function but for examples simplicity is good. What is truly useful about functions is that they can "return" a value, much like a variable, to be used wherever they are put into the code.
In BASIC, actually, functions must be used like variables. Most programming languages only have functions, and they can be used like statements or like variables. BASIC on the other hand uses subroutines for statements, and functions for variables. Please note, functions are not variables! Functions cannot be assigned a value. The analogy accurately describes only the way to use the two different types. I will refer only to functions from here on, but where BASI is concerned a subroutine may really be what I am describing. Now for a better example of a function.
FUNCTION CircleArea (r) CircleArea = (r * r) * 3.14 END FUNCTION PRINT CircleArea(5)
As is rather clear this function calculates the area of a circle, given its radius. Note that to return the value, it is assigned to an imaginary variable with the same name of the function. So when we get to the print line, we pass in 5 to the CircleArea function, the function fills out the formula and retuns the value. It is just like we had typed the answer instead of the function, it prints right out. This way we got to have the computer do the hard math. Phew.
Before you try to use functions on your own, you'll need to learn one special thing. QBASIC handles functions in a way that can help you organize long programs but can be confusing at first. Each function gets its own workspce separate from the rest of the program. Pressing F2 will give you a list of these workspaces. Typing out a FUNCTION line and pressing enter triggers this behavior. As soon as QBASIC sees a line that starts a function, it does two things for you. It automatically puts the line to end the function on, then moves the whole new function onto a new workspace named after the function. Remember, when you are done here press F2 to go back to the main program (or another function).
There's much more to know about functions, but they're for advanced programs more than basic ones. We'll learn more about them in the future. We're going to learn some totally new topics next.