Begin Programming Logo
Home | KPL | Object Pascal | General Programming | Code Database | About Us
   
Methods and Functions
   
   

A procedure is like a 'subprogram'. When your program runs, the computer goes to 'method Main()' and then goes down each line of code, one by one until it gets to 'End Method'. However, what a procedure does is that when the computer gets to the name of the procedure in your code, it will 'jump' to another part of your program and then when it has finished with that it will jump back.

Methods are not written inside the ‘Method Main() block’, but instead below or above it.  They all follow the format ‘Method NameOfMethod()’.  You must remember to put the parenthesis (brackets) after the name of the method.  Whatever you call the method should not be abbreviated and should be descriptive of what the method does.

To run a method from your program, simply write the method’s name and then ().

Methods are used for two reasons, firstly if there is code which you use in various different parts of your game, rather than copying and pasting a section of code, if you procedurise the repeat section, then you can simply call (or run) that procedure, meaning that if you have to change your code, you only have to do it once.  The other reason is that having one long line of code is far more difficult to understand than having procedure names, for example:

      Method Main()
      LoadMainSprite()
      DrawBackground()
      LoadBaddies()
      HasCrashed = False
      XCoordinateofMainSprite = 20
      YCoordinateofMainSprite = 200
      While HasCrashed = False
                  CheckForAnArrowKey()
                  Delay(10)
                  DetectACrash()
                  AnimateBaddies()
      End While
End Method

This above code is the main method for a full game which I have made.  By looking at this short section of code, you very quickly can understand exactly how my game works.  When you want to understand how the code for my game works, it does not matter to you which lines of code I have used to draw the background, you just need to know that I have drawn it.  Imagine trying to follow the code for the entire game, which is over 100 lines long, it is a lot more difficult than just reading method names.

As a rule of thumb, methods should only do one single task, for example a method could draw the background or load the sprites, but it couldn’t draw the background AND load the sprites, as these are two separate actions.  Methods should have at least a few lines of code in them…having a method with only one line of code doesn’t make the program easier to read or save the number of lines you have to type!

It is often the case that you will want to use a variable inside a procedure.  There are 3 ways to do that.

1) If the variable is only going to be used inside a specific method, then you can declare the variable inside the method.
For example
Source code showing a local variable
Here, the variable count is only used inside the Method LoadSprites.  It is not used outside this method, therefore we can and should declare it inside ‘Method LoadSprites()’.  These are called local variables.

2) If you need to use a variable in more than one method, the above will not work.  One way we can get around this is by using global variables.  A global variable can be accessed by all methods.  You declare global variables outside of any methods, just below ‘Program NewProgram’
Source code showing a global variable

In this example, we use the variable ‘Count’ in two methods ‘LoadSprites()’ and ‘Main()’.  We have therefore have declared Count outside of any method, so that all methods can access it.

However, this is not considered to be very “nice” and most computer programmers will discourage the use of global variables. 

3) Instead of global variables, we can use local variables in the first method, and then when we call the second method, we can “give it” the local variable.  To do this method, we first of all create a local variable in the first method.  Then, when we call the second method, we put the variable name inside the parenthesis.  By writing the variable name inside parenthesis, we are telling KPL then when it runs LoadSprites to pass it the count . eg

Define Count As Int
For Count = 1 To 5
     LoadSprites(Count)
Next

We also need to tell the method being called that it is to expect to receive a variable, and what type of variable it will receive, so that the second method can effectively ‘put out a hand’ with which to receive the variable.  This is done by writing inside the brackets in ‘Method LoadSprites()’.  In the brackets we have to declare the variable again, but miss out the word ‘Define’. Therefore you need to write the name of the variable, the word as and the type of variable which you are passing in.  The name which you put in the brackets should not be the same as the original variable name.  Inside the method being called, you should use the name in the brackets, not the original variable name.

Method Main()
     Define Count As Int
     For Count = 1 To 5
          LoadSprites(Count)
     Next
End Method

Method LoadSprites(SpriteNumber As Int)
     LoadSprite(SpriteNumber, "UFO.gif")
     MoveSpriteToPoint(SpriteNumber, SpriteNumber * 100, SpriteNumber * 100)
     ShowSprite(SpriteNumber)
End Method

In this example, we create a local variable in ‘Method Main()’ called Count.  We then pass it into ‘Method LoadSprites’, however inside ‘Method LoadSprites’ we will call variable Count, ‘SpriteNumber’ although we know that Count and SpriteNumber are really the same variable.

   

Valid XHTML 1.0 Transitional Valid CSS!