In this course, we'll be revising through Procedures And Functions, both basic concepts of programming. These help to modulate code, which makes debugging easier.
What are Procedures and Functions?
Procedures? Functions? What? Is this some sort of calculator manual? No, procedures and functions are features of any programming language which allow you to repeat a certain piece of code or calculation again and again. They also help modulate code, so that they can be called from many places in a program.
First of all, what is a procedure? We'll use an analogy here: Let's imagine that you're a dishwasher. Your process of washing a dish could be:
Now about functions. A function is just like a procedure except that it returns a value. For example, somebody may ask you to count the number of chairs in a hall. You would first step in, count, and then report the number of chairs in the hall to the person who first asked you. That is a function. A function simply returns another value back into the program, such as complex calculation results, the position of the mouse cursor, or the number of times an atomic bomb had dropped onto the Earth so far. Functions can report almost anything. Numbers, strings, characters, anything! And if you want to, you can even use functions to replace procedures completely. This is the case in some languages where there are no special implementations of procedures.
Scope of Variables
Procedures and Functions have a catch: They have the concept of scope. To understand this concept, visualize a stack of exercise books on a table. Everytime you call a procedure or a function, it will be placed at the top of the stack. When the procedure or function finishes, it is removed. Now, when a procedure and function goes, everything declared in it goes too. That means that if you had declared a variable in a procedure, you can't use that variable after the procedure finishes. The variable simply goes poof, (and disappears into a mist of sparkling electronic dust). Variables however, are accessible to those procedures placed above it.
End Of Course
Now that you have revised all the basic concepts, you may learn about how to implement them in the next course. After that, be sure to look through our miscellaneous tips in "Miscellaneous Tips", where you'll be introduced to other aspects of Pascal programming.