At the end of this lesson, you will know…
- What functions, procedures, and methods are
- How functions can call other functions
What are functions, procedures, and methods?
These are just the names we use to call about a small piece of the program that does a particular task.
That task may be to perform a mathematical calculation on some numbers, or read some data from a file, or place an order on a website. They can perform huge tasks or small ones. However, if the task/function is large, we usually break it down into smaller tasks/functions. This makes them easier to read.
Personally, I try to keep my functions no longer than 15 – 20 lines of code – at the max. Sometimes, it makes sense to have larger ones. But it’s easier to figure out what a function is doing if you can see all of it on one screen, without constantly scrolling up and down.
Nowadays, most people use these names interchangeably. In the past, programmers would often use “function” for a piece of the program that did something and returned a value. Whereas a “method” did something, but didn’t return any value.
If you look at the World.cs class we created, it has eight methods – if you don’t count the constructor method.
Methods with no return values
The first four are the ones that start with “Populate”. Let’s take a look at the “PopulateItems” method.
private static void PopulateItems()
Items.Add(new Weapon(ITEM_ID_RUSTY_SWORD, "Rusty sword", "Rusty swords", 0, 5));
Items.Add(new Item(ITEM_ID_RAT_TAIL, "Rat tail", "Rat tails"));
Items.Add(new Item(ITEM_ID_PIECE_OF_FUR, "Piece of fur", "Pieces of fur"));
Items.Add(new Item(ITEM_ID_SNAKE_FANG, "Snake fang", "Snake fangs"));
Items.Add(new Item(ITEM_ID_SNAKESKIN, "Snakeskin", "Snakeskins"));
Items.Add(new Weapon(ITEM_ID_CLUB, "Club", "Clubs", 3, 10));
Items.Add(new HealingPotion(ITEM_ID_HEALING_POTION, "Healing potion", "Healing potions", 5));
Items.Add(new Item(ITEM_ID_SPIDER_FANG, "Spider fang", "Spider fangs"));
Items.Add(new Item(ITEM_ID_SPIDER_SILK, "Spider silk", "Spider silks"));
Items.Add(new Item(ITEM_ID_ADVENTURER_PASS, "Adventurer pass", "Adventurer passes"));
First, we have the scope “private”.
“Private” means that this method can only be run by other code inside this class. If it was “public”, then it could be run from other classes. But we don’t need to allow that for this method, since it’s only ever run once, from this class’ constructor.
Next, we have “static”.
Normally, when you want to run a method, you do it on an object. For example, on a monster object, you might have a ReceiveDamage method that gets the amount of damage that was done to the monster, and subtracts that from its CurrentHitPoints. The method is doing something to, or with, the object.
If you want to run a method without instantiating an object from the class, then the method needs to be static. Since this method is in a static class, which can never be instantiated, we need to include static here.
Then, there is “void”.
“Void” means that this method is not going to return a value. It’s just going to do some work. In this case, the work is to populate the Items list with the items in our game world.
Finally, we have the method name “PopulateItems”.
Give your methods names that are easy to understand. Then, when you, or another programmer, want to make changes to the program in the future, it will be easier to figure out what the method is supposed to be doing.
It’s also common to name methods in a verb-noun format. What are you doing, and what are you doing it to?
Methods that return a value
Now, let’s take a look at a method that returns something, the “ItemByID” method.
public static Item ItemByID(int id)
foreach(Item item in Items)
if(item.ID == id)
This time, the scope of the method is “public”.
We’re going to need to “call” (a common way to say “run” or “execute” for a method) this method from other parts of the game. So, it needs to be public.
We still need “static”, since this class is never an object.
Now, instead of “void”, we have “Item”.
That’s because this method is going to return a value, and the datatype of that value is going to be “Item”. So, the user knows that when they call this method, they should get an Item back.
This method also accepts parameters. Just like we had in the constructors of our other classes. We’ll talk later about exactly what is happening inside the method. But, for now, you can probably guess that the method accepts an ID number and returns the item with the matching ID number.
Methods that call other methods
Looking at the code in the constructor of this class, we see that all it has in it is the name of the Populate methods in the class. That’s because all this method does is run those other methods.
When the constructor is called, it runs the PopulateItems method, then the PopulateMonsters, PopulateQuests, and PopulateLocations methods.
When you run your program, the computer is basically performing one line of the program at a time – at least, until you get into multi-threaded programming. So picture the computer working like this:
- It starts to run the World constructor
- It see the line “PopulateItems();” and goes to the PopulateItems method
- It goes through each line in the PopulateItems method, adding items to the list
- When it reaches the end of the PopulateItems method, it returns back to where it was last in the World constructor
- The next line is “PopulateMonsters();”, so the computer goes to the PopulateMonsters method and performs each line in there
- At the end of the PopulateMonsters method, it returns to the World method and performs the next line “PopulateQuests();”
So, a line of code in one method can call another method, which can contain a line of code that calls another method, which contains a line of code that calls another method, and so on…
In large programs, these method calls can get 20 layers deep, or deeper. That can make it difficult to find bugs, since you aren’t really sure what happened in each layer. So, remember to keep your programs simple. Don’t add another layer, just because you can.
Now you understand the basics of functions, procedures, and methods.
These are important, because you need to create them to start doing things in the program. We’ll have some that handle user input, some that do let the player move around, and some that handle our game’s battles. These will be the brains of our program.
Source code for this lesson
No source code for this lesson
Previous lesson: Lesson 12.1 – Add the remaining UI controls
All lessons: Learn C# by Building a Simple RPG Index