Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 5 METHODS

Parameters

So far, you’ve seen that methods are named units of code that can be called from many places in a program and can return a single value to the calling code. Returning a single value is certainly valuable, but what if you need to return multiple values? Also, it would be useful to be able to pass data into a method when it starts execution. Parameters are special variables that allow you to do both these things.

Formal Parameters

Formal parameters are local variables that are declared in the method declaration’s parameter list, rather than in the body of the method.

The following method header shows the syntax of parameter declarations. It declares two formal parameters—one of type int and the other of type float.

public void PrintSum( int x, float y )

{

 

 

...

Formal parameter declarations

}

 

 

 

Because formal parameters are variables, they have a data type and a name, and they can be written to and read from.

Unlike a method’s other local variables, the parameters are defined outside the method body and are initialized before the method starts, except for one type, called output parameters, which I’ll cover shortly.

The parameter list can have any number of formal parameter declarations, and the declarations must be separated by commas.

The formal parameters are used throughout the method body, for the most part, just like other local variables. For example, the following declaration of method PrintSum uses two formal parameters, x and y, and a local variable, sum, all of which are of type int.

public void PrintSum( int x, int y )

{

int sum = x + y;

Console.WriteLine("Newsflash: {0} + {1} is {2}", x, y, sum);

}

80

CHAPTER 5 METHODS

Actual Parameters

When your code calls a method, the values of the formal parameters must be initialized before the code in the method begins execution.

The expressions or variables used to initialize the formal parameters are called the actual parameters. They are also sometimes called arguments.

The actual parameters are placed in the parameter list of the method invocation.

Each actual parameter must match the type of the corresponding formal parameter, or the compiler must be able to implicitly convert the actual parameter to that type. I’ll explain the details of conversion from one type to another in Chapter 18.

For example, the following code shows the invocation of method PrintSum, which has two actual parameters of data type int:

PrintSum( 5, someInt );

Expression

Variable of type int

When the method is called, the value of each actual parameter is used to initialize the corresponding formal parameter. The method body is then executed. Figure 5-6 illustrates the relationship between the actual parameters and the formal parameters.

Figure 5-6. Actual parameters initialize the corresponding formal parameters.

Notice that in the previous example code, and in Figure 5-6, the number of actual parameters must be the same as the number of formal parameters (with the exception of params parameters, which I’ll discuss later). Parameters that follow this pattern are called positional parameters. We’ll look at some other options shortly.

81

CHAPTER 5 METHODS

An Example of Methods with Positional 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 floats and returns their average. In the second invocation, notice that the compiler has implicitly converted the two int values—5 and someInt—to the float type.

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 Program

{

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

82

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]