Information Technology Reference
In-Depth Information
An Example of Methods with Input Parameters
In the following code, class MyClass declares two methods—one that takes two integers and
returns their sum, and another that takes two float s and returns their average.
class MyClass Formal parameters
{
public int Sum(int x, int y) // Declare the method.
{
return x + y; // Return the sum.
} Formal parameters
public float Avg(float Input1, float Input2) // Declare the method.
{
return (Input1 + Input2) / 2.0F; // Return the average.
}
}
class Class1
{
static void Main()
{
MyClass MyT = new MyClass();
int SomeInt = 6;
Console.WriteLine
("Newsflash: Sum: {0} and {1} is {2}",
5, SomeInt, MyT.Sum( 5, SomeInt )); // Invoke the method.
Console.WriteLine Actual parameters
("Newsflash: Avg: {0} and {1} is {2}",
5, SomeInt, MyT.Avg( 5, SomeInt )); // Invoke the method.
}
} Actual parameters
This code produces the following output:
Newsflash: Sum: 5 and 6 is 11
Newsflash: Avg: 5 and 6 is 5.5
Search WWH ::




Custom Search