parameters & return

Parameters allow us to pass data into a method.
We can also pass value out of a method.

Value Parameters
Normally, when a function is called, a COPY of the argument is made in the called function... (rob miles)
       // read a valid double value within the specified range
       // loop until good value within range as specified by user
       static double readValue ( string prompt, // prompt for the user 
                                 double low,    // lowest allowed value 
                                 double high )  // highest allowed value
           bool ok;
           double result = 0; 
               System.Console.WriteLine( prompt + " between " + low + " and " + high ); 
               ok = double.TryParse( System.Console.ReadLine( ), out result ); 
           } while ( (result < low) || (result > high) || ok == false ); 
           return result ; 

   Use the method:
      // pass parameters by value
      double windowWidth = readValue ( "Enter width of window: ", MIN_WIDTH, MAX_WIDTH); 
      // pass by value
      double age = readValue ( "Enter your age: ", 0, 70);
Listing 4.4: Declaring a Method - essential c# 4.0
   class IntroducingMethods
       static void Main()
           string firstName;
           string lastName;
           string fullName;
           // get names (simple)
           System.Console.WriteLine( "Hey you!" );
           firstName = GetUserInput( "Enter your first name: " );
           lastName = GetUserInput( "Enter your last name: " );
           fullName = GetFullName( firstName, lastName );

           DisplayGreeting( fullName );
           // get names (complicated)
           System.Console.WriteLine( "Hey you!" );
           fullName = GetFullName( GetUserInput( "Enter your first name: " ), GetUserInput( "Enter your last name: " ));           
           DisplayGreeting( fullName );
       // static methods belong to the class not an object
       static string GetUserInput(string prompt)
           System.Console.Write( prompt );
           return System.Console.ReadLine( );
       static string GetFullName(string firstName, string lastName)
           return firstName + " " + lastName;

       static void DisplayGreeting(string name)
           System.Console.WriteLine( "Your full name is " + name + "." );

Reference Params
Reference parameters are useful in three situations: 1. Returning more than one value 2. Changing the actual argument passed in 3. When passing the address would save memory space and time (arrays) In pass by reference, the address of the arg is passed to function. ^^^^^^^ This is denoted by the use of "ref" immediately before the type in BOTH the call & the function definition.
       // demo pass by reference
       class Program
           static void Main( )
               int a = 1;
               int b = 2;

               // output, swap and then output again
               Console.WriteLine("a is " + a + " b is " + b);
               Swap(ref a, ref b);
               Console.WriteLine("a is {0} b is {1}", a, b);
           // swaps the two int parameters passed in
           static void Swap(ref int i, ref int j)
               int temp = j;
               j = i;
               i = temp;
Swap DOES HAVE AN effect on the original arguments in the calling function.

Out parameters
Out params are useful when the called method provides a value for the parameter. The incoming value in the parameter is meaningless and C# relaxes the requirement that it need have a value assigned.
   bool ok = int.TryParse(Console.ReadLine(), out num);
Similar to "ref", but the calling method does NOT set a value to the parameter.

Named arguments and Optional/default params
Named arguments free you from the need to remember the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. [msdn:] A function that calculates body mass index (BMI) can be called in the standard way by sending arguments for weight and height by position, in the order defined by the function.
   CalculateBMI(123, 64);

If you do not remember the order of the parameters but you do know their names, you can send the arguments in either order, weight first or height first.
   CalculateBMI(weight: 123, height: 64);
   CalculateBMI(height: 64, weight: 123);

Named arguments also improve the readability of your code by identifying what each argument represents. A named argument can follow positional arguments, as shown here.

   CalculateBMI(123, height: 64);

However, a positional argument cannot follow a named argument. The following statement causes a compiler error.

   CalculateBMI(weight: 123, 64);


Optional parameters must occur at the end of param list. Must be given a value.

   static void ExampleMethod( int required, 
                              string optionalstr = "default string",
                              int optionalint = 10 )
Default parameters simply give a value in the param list. Any supplied value overrides the default.
   static void ExampleMethod( int required = 0)