Procedures and Functions

From now on I will assume you will be able to turn a simple or basic idea into a program. For example if I said I wanted a program that will write out the even Numbers from 1 to 150 you will be able to do it. So from now on I won't be writing much code. This will save me time, and you time, since you will be able to read the pseudo-code and understand what is happening better then if I had actual Pascal code on the screen.

So lets say I want to write a program which will run a robot. Now this program will basiclly make the robot perform as if it were a real person, and I want to write it so that it will wake up, go though a normal day, then go to sleep. So my basic plan for the program will be as follows.

Begin
  Wake_up;
  Have_a_Shower;
  Eat_Breakfast;
  Go_to_School;
  Go_to_Math_Class;
  Go_for_Lunch;
  Go_to_Computer_Class;
  Go_Teach_CPSC_321;
  Go_Home;
  Eat_Diner;
  Mark_Assignments;
  Go_to_Sleep;
End.
Now the code to wake up may be simple code, or may take a year to write, we are not to concerned with that, we can just assume we can write it. Now we wake Up and go to the shower, but its not really that simple. For example, what if someone just had a shower and thers no more hot water, then I may go eat breakfast first, then go have a shower. So now our code looks like this...
Begin
  Wake_up;
  If(hot water) then begin
     Have_a_Shower;
     Eat_Breakfast;
  End
  Else begin
     Eat_Breakfast;
     Have_a_Shower;
  End;
  ....
  ....
End.
So here we need to type in 2 copies of the code for having a shower and eating breakfast. Now if we were to use our imaginations we can see that if we are going to make a fully interactive robot to replace me we will need a lot of code. So what is the problem with that? Once it is compiled it doesn't need to be compiled again, so make it as big as you can, makes no difference right? Well wrong. You see there are problems with size, so remember, size does matter, there are also problems with changes and errors. If the code to have a shower exists in 5 places then there are 5 places of code that need to be changed. This can lead to problems from missing areas to change ect.

So how do we make this all so much simplier? We use procedures (and functions).

Procedures
A procedure is a set of instructions that can be executed at any time, for example Have_a_Shower. We can do this infact..

Procedure Have_a_Shower;
Begin
  Code_for_a_Shower;
End;

Procedure Eat_Breakfast;
Begin
  Code_for_Eating_Breakfast;
End;

Begin
  Wake_up;
  If(hot water) then begin
     Have_a_Shower;
     Eat_Breakfast;
  End
  Else begin
     Eat_Breakfast;
     Have_a_Shower;
  End;
  ....
  ....
End.
Now what is the difference then before? Well instead of having the code for having a shower in several places, we just have it one place and then tell the program to go execute the code there. It is the same code so only 1 copy of it exists and only one copy needs to be changed or edited when the case arrives.

Functions
A function is very much like a procedure, except the function produces something. For example, Taking_Notes; is a function, since when I am done I have notes as an end product. However, Go_to_School; is a procedure because there is nothing produced.

Functions are useful because they allow us to do the same thing in many places and give us a value. Sometimes they can indicate success or failure. For example I can change my Go_to_School; procedure to a function by having it Return the value 1 if the robot sucessfully got to the school, or 0 if it failed (ran out of gas, stuck in trafic, abducted by aliens, ect).