3.3. Method Parameters
As a young child, you probably heard the “Old MacDonald” song. Consider the following two verses:
Verse 1 |
Verse 2 |
---|---|
Old MacDonald had a farm |
Old MacDonald had a farm |
E-I-E-I-O |
E-I-E-I-O |
And on that farm he had a cow |
And on that farm he had a duck |
E-I-E-I-O |
E-I-E-I-O |
With a moo-moo here |
With a quack-quack here |
And a moo-moo there |
And a quack-quack there |
Here a moo, there a moo |
Here a quack, there a quack |
Everywhere a moo-moo |
Everywhere a quack-quack |
Old MacDonald had a farm |
Old MacDonald had a farm |
E-I-E-I-O |
E-I-E-I-O |
As you can see, the two verses are in most part identical. We can
probably create a method named Verse
to abstract the
repetitive lines, but the method will need two parameters,
which are placeholders that allow different values to be substituted
for the animal and noise when the method is called. The method body
will use the parameter as variables to customize the print statements.
internal class Program
{
private static void Main(string[] args)
{
// Console.WriteLine("Hello, World!"); // this statement is commented out
Verse("chicken", "buk"); // method call
}
public static void Verse( String animal, String noise ) // method signature; ``parameters with types``
{
Console.WriteLine( "Old MacDonald had a farm" ); // method body starting here
Console.WriteLine( "E-I-E-I-O" );
Console.WriteLine( "And on that farm he had a " + animal );
Console.WriteLine( "E-I-E-I-O" );
Console.WriteLine( "With a " + noise + "-" + noise + " here") ;
Console.WriteLine( "And a " + noise + "-" + noise + " there" );
Console.WriteLine( "Here a " + noise + ", there a " + noise );
Console.WriteLine( "Everywhere a " + noise + "-" + noise );
Console.WriteLine( "Old MacDonald had a farm" );
Console.WriteLine( "E-I-E-I-O" );
}
}
By now, you should be pretty familiar with the process of creating a project, writing some short code, and test the outcome. As a review, the process should look like the follows.
tychen@mac:~/workspace/introcscs$ mkdir Verse
tychen@mac:~/workspace/introcscs$ cd Verse/
tychen@mac:~/workspace/introcscs/Verse$ dotnet new console --use-program-main
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/tychen/GoogleDrive/workspaces/workspace/introcscs/Verse/Verse.csproj:
Determining projects to restore...
Restored /Users/tychen/GoogleDrive/workspaces/workspace/introcscs/Verse/Verse.csproj (in 176 ms).
Restore succeeded.
tychen@mac:~/workspace/introcscs/Verse$ code .
In VS Code, you could edit the code and run it from the terminal in VS Code by issuing
dotnet run
. Alternatively, you may run your code in terminal and even use editors such
as nano
to edit your code and run the project in the terminal directly.
The result of the preceding program should look like the following:
Old MacDonald had a farm
E-I-E-I-O
And on that farm he had a chicken
E-I-E-I-O
With a buk-buk here
And a buk-buk there
Here a buk, there a buk
Everywhere a buk-buk
Old MacDonald had a farm
E-I-E-I-O
Let us review the standard syntax method signature template:
Access Modifier [static modifier] Return Type MethodName(Parameter List)
{
Method Body
}
In this program, when you call the Verse method, you provide values between the
parentheses, called arguments
, that are copied into the parameters
(parameter list in the parentheses after the method name) to
be used in the method body as variables
. If you call the method more than one time
and supply different arguments, each method call will be run with the arguments
supplied, such as in:
Verse( "cow", "moo" );
Verse( "duck", "quack" );
The main method will call the verse method twice, once for the cow and once for the duck.
3.3.1. Variable Scopes
A variable may be available for use in some parts of code, but not others.
The scope
of a variable is the region of the program that the variable is visible,
which means it is accessible by name
and can be used in the part of code. C# scope rules of variables can be divided into three categories as follows:
Class Level Scope
Method Level Scope
Block Level Scope.
A variable declared inside a method is a local variable
. The scope of
a local variable is the method body
in which it is declared. You can’t use
a variable before it is declared, so in fact the scope begins on the line that
declares the variable and continues until the last line of code in the method.
The local variable’s memory location is only available while the method is
executing. When the method completes, the memory location is released.
If you called the method again, the old value is not available.
Note
Only the value of the actual parameter is passed, not any variable name, so there is no need to have a match between a variable name (arguments) and the parameter name.
3.3.2. Static Variables
You may define static variables
(variables defined with the word static
inside the class, but outside of any method definition). These variables are
visible inside all of your methods in the class. Instead of local scope, static variables have class scope.
It is good programming practice generally to avoid defining static variables and
instead to put your variables inside methods and explicitly pass
them as parameters where needed. An example for using static variables is constants:
A constant is a name that you give a fixed data value to and you do not expect the value to change
(e.g., Pi).
3.3.3. Method Tracing
A method can call other methods to help it do its job. Consider the following code:
public static void inchesToCentimeters(double i) // parameter with type
{
double c = i * 2.54;
printInCentimeters(i, c);
}
public static void printInCentimeters(double inches, double centimeters)
{
Console.WriteLine(inches + "-->" + centimeters);
}
public static void main(String[] args)
{
inchesToCentimeters(10);
}
What is printed when the main method is run?