Function Parameters
When a function is to accept parameters, you must specify the following: A list of the parameters accepted by the function in its definition, along with the types of those parameters .
A matching list of parameters in each function call . This involves the following code, where you can have any number of parameters, each with a type and
a name:
static < returnType > < functionName > ( < paramType > < paramName > , …)
{
…
return < returnValue > ;
}
The parameters are separated using commas, and each of these parameters is accessible from code within the function as a variable. For example, a simple function might take two double parameters and return their product:
static double Product(double param1, double param2) { return param1 * param2; }
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static int MaxValue(int[] intArray) { int maxVal = intArray[0]; for (int i = 1; i < intArray.Length; i++) { if (intArray[i] > maxVal) maxVal = intArray[i]; } return maxVal; } static void Main(string[] args) { int[] myArray = { 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 }; int maxVal = MaxValue(myArray); Console.WriteLine("The maximum value in myArray is {0}", maxVal); Console.ReadKey(); } } }