Let’s imagine you get the urge to have the computer write the following to the screen:
Hello Jo
Hello Sam
Hello You
Here’s one way to do it … we could write a separate method for every situation:
| void WriteHelloJo()
{
Console.WriteLine(“Hello Jo”);
}
void WriteHelloSam()
{
Console.WriteLine(“Hello Sam”);
}
void WriteHelloYou()
{
Console.WriteLine(“Hello You”);
} |
and then call the methods like this :
| WriteHelloJo();
WriteHelloSam();
WriteHelloYou(); |
But that seems like rather a waste of energy since the three methods are all almost the same. What if we could use one WriteHello method and, each time we call it, just tell it the part that needs to be different.
This can be done by writing the method as follows :
| void WriteHello( string someName )
{
Console.WriteLine( “Hello ” + someName );
} |
and then calling it like this :
| WriteHello( “Jo” );
WriteHello( “Sam” );
WriteHello( “You” ); |
As you can see this saves both space and effort. You should always try to write as little code as possible. Generally speaking, the shorter the program is, the smarter is the programmer.
When we write the method in this smart new way,
| void WriteHello( string someName )
{
Console.WriteLine( “Hello ” + someName );
} |
we’re really saying “Whenever I call this method, I’ll pass it a string of letters holding some name. Whatever that thing is that I pass you, I want you to write after the word “Hello”.
The thing in the parenthesis (string someName) is called a parameter. It allows you to pass a value into the method when you call it.
After all, when your teacher taught you how to add numbers, she didn't teach you every single possibility - she taught you the method and then threw a whole bunch of different problems at you: "Add 2 and 5. Now add 7 and 3." It's as if she gave you a method called AddNumbers and passed you different parameters each time. No matter what parameters she sent you, you were able to work out the answer because you knew the method.
The computer could care less what name you give to a parameter – but is very fussy about your using the same name throughout your method. This, for example will work correctly:
| void WriteHello( string x )
{
Console.WriteLine( “Hello ” + x );
} |
But this will not :
| void WriteHello( string someName )
{
Console.WriteLine( “Hello ” + someBodysName );
} |
Can you see the mistake? "someName" and "someBodysName" are different - this will confuse our binary friend and cause it to throw a tantrum.
It is also perfectly legal to have more than one parameter in a method, you just need to separate them with commas:
| void WriteHello( string firstName, string lastName )
{
Console.WriteLine( “Hello ” + firstName + “ ” + lastName );
} |
But then you must pass in the right number of values when you call the method
| WriteHello( “Jiminy”, “Cricket” ); |
This will, of course, write out to the screen “Hello Jiminy Cricket”.
Sending Wrong Parameter Types
Suppose that, back when you first learned how to add numbers, your teacher suddenly threw this problem at you: "Add the numbers 5 and flower".
How would you have responded? Probably something like this: "Flower is not a number! You can't add those." Quite right.
In the same way, the computer will complain bitterly if you send it a value of the wrong type. This is a common programming error. So if things aren't working as you'd expected, go back and check that you're sending values whose types match those defined in the method.
Building Block : Parameters |
If we want to pass specific values into a method, we define parameters in the method : | class Person
{
// Fields
string firstName;
string lastName;
// Method
public void LuckyNumber( int numberOfTeeth, int age )
{
Console.WriteLine( “Lucky number is “ + numberOfTeeth * age );
}
…
} |
And then, whenever we call the method, we make sure we pass the right kind of values into those parameters. In the example below we pass in two integer numbers because the parameters for the method “LuckyNumber” were defined as integers. | Person jiminy;
jiminy = new Person();
jiminy.LuckyNumber( 24, 14 ); |
|