Home
Introduction
Courses
Glossary
About

Beginner - Procedures And Functions

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.

Main Program -> Procedure -> Main 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:

  1. Dip the dish into the water
  2. Cover every inch of the dish with soap
  3. Rinse and dry the dish
So, every time you need to wash a dish, you do just that. Dip, soap, dry. Dip, soap, dry. Dip, soap, dry. Even when you go home, you dip, soap, dry. Dip, soap, dry. It's the same sequence, repeated over and over again. A procedure works the same way. People replace the process of dipping, soaping, and drying with the command "wash the dishes". When you call a procedure, it simply does the jobs that the procedure is supposed to do. By replacing a stack of instructions with one single statement, if makes code easier to read and debug.

Main Program -> Function -> 6 -> Main Program

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 are 'stacked'

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.

PreviousNextTop