C# Functions Part 2


Function Parameters.

class Program  // Function Parameters
{ 
//
    // pass in an integer array and return an integer
    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;  // must have return statement when not void
    }
    static void MySorting(ref int[] intputArray)
    {   // just for illustration purposes
        // don't need to write your own function 
        Array.Sort(intputArray);
    }
    static void Main(string[] args)
    {
        int[] myArray = { 1, 8, 3, 6, 9 };
        int maxVal = MaxValue(myArray);
        WriteLine(maxVal);
        MySorting(ref myArray);  // ref - the default is reference
        foreach (int i in myArray) Console.Write(i + " ");
        ReadKey();
    }
}

Functions Part 3

Leave a comment

Your email address will not be published. Required fields are marked *