640 likes | 798 Views
Method Parameters and Overloading. Topics. The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods. Objectives. At the completion of this topic, students should be able to:. Correctly write methods that use pass by value
E N D
Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods
Objectives At the completion of this topic, students should be able to: Correctly write methods that use pass by value Correctly write methods that use pass by reference Explain what a side effect is Explain what method overloading is, and correctly use method overloading in a program Explain how type conversion affects method overloading Explain what a Driver and a Stub methodare, and use them in programs
The Execution or Run-Time Stack An important component in understanding how methods work is the execution or run-time stack. The following slides discuss how C# uses the run-time stack when invoking a method. Note that this is only a conceptual view of how the stack operates. It is slightly more complicated than what is shown here, and operation of the stack depends a great deal on the operating system, the compiler, and the hardware environment.
To get an idea of how the stack works, think of the plate dispensers that you have seen in a cafeteria When a plate is removed from the top of the stack, all of the other plates pop up. When a plate is pushed onto the stack, all of the other plates get pushed down.
When a method is called (invoked), the computer builds a stack frame. The stack frame contains * the parameters that are being passed to the method * the address to return to when the method is done * any local variables declared in the method
Any variables declared inside main’s { } local variables control returns to the operating system Stack frame for Main( ) return address Main( )’s parameters come from the command line parameters The Stack
Main( ) calls method ”B” any variables declared inside of B’s { } local variables Stack frame for method “B” returns to the point where B was called from inside of Main return address any parameters passed to B parameters local variables Stack frame for Main( ) return address parameters The Stack
When method “B” is done, its stack frame is removed from the stack. local variables Stack frame for Main( ) return address parameters The Stack
local variables Main( ) now goes on about its work. Stack frame for Main( ) return address parameters The Stack
using System; class Program { static void Main() { int a = 5; int b = 3; int c = Add(a,b); Console.WriteLine("The answer is {0}", c); Console.ReadLine( ); }//End Main() static int Add(int num1, int num2) { int sum = num1 + num2; return sum; } }//End class Program
static void Main() { int a = 5; int b = 3; int c = Add(a,b); Console.WriteLine("The answer is {0}", c); Console.ReadLine( ); } a = 5 b = 3 return address no parameters The Stack
static void Main() { int a = 5; int b = 3; int c = Add(a,b); Console.WriteLine("The answer is {0}", c); Console.ReadLine( ); } a = 5 b = 3 return address no parameters The Stack
Build a Stack Frame for the call of Method B and push it onto the stack local variables static void Main() { int a = 5; int b = 3; int c = Add(a,b); Console.WriteLine("The answer is {0}", c); Console.ReadLine( ); } return address parameters a = 5 b = 3 Main’s Stack Frame return address no parameters The Stack
The return address that goes on the stack is right here … before the assignment part of this statement. local variables static void Main() { int a = 5; int b = 3; int c = Add(a,b); Console.WriteLine("The answer is {0}", c); Console.ReadLine( ); } a = 5 b = 3 B’s Stack Frame return address parameters return address a = 5 b = 3 no parameters Main’s Stack Frame return address no parameters The Stack The Stack
Put the parameters in the stack frame. These are copies of the values stored in a and b. local variables static void Main() { int a = 5; int b = 3; int c = Add(a,b); Console.WriteLine("The answer is {0}", c); Console.ReadLine( ); } a = 5 b = 3 B’s Stack Frame return address return address 3 5 3 5 return address a = 5 b = 3 a = 5 b = 3 no parameters Main’s Stack Frame return address return address no parameters no parameters The Stack
Sum is a local variable declared inside of the Add method In the Add method, we use these names to refer to the parameters that were passed to the method. sum a = 5 b = 3 B’s Stack Frame return address return address 3 5 num2 3 5 static int Add(intnum1, int num2) { int sum; sum = num1 + num2; return sum, } return address num1 a = 5 b = 3 a = 5 b = 3 no parameters Main’s Stack Frame return address return address no parameters no parameters The Stack
Sum is a local variable declared inside of the Add method 8 sum a = 5 b = 3 B’s Stack Frame return address return address 3 5 num2 3 5 static int Add(intnum1, int num2) { int sum; sum = num1 + num2; return sum, } return address num1 a = 5 b = 3 a = 5 b = 3 no parameters Main’s Stack Frame return address return address no parameters no parameters The Stack
Sum is a local variable declared inside of the Add method Copy the value of sum into the eax register 8 8 sum a = 5 b = 3 B’s Stack Frame return address return address 3 5 num2 3 5 static int Add(intnum1, int num2) { int sum; sum = num1 + num2; return sum, } return address num1 a = 5 b = 3 a = 5 b = 3 no parameters Main’s Stack Frame return address return address no parameters no parameters Values returned from a method are passed in a special hardware register The Stack eax
Get the return address 8 8 sum a = 5 b = 3 B’s Stack Frame return address return address 3 5 num2 3 5 static int Add(intnum1, int num2) { int sum; sum = num1 + num2; return sum, } return address num1 a = 5 b = 3 a = 5 b = 3 no parameters Main’s Stack Frame return address return address no parameters no parameters Values returned from a method are passed in a special hardware register The Stack eax 8
Remove B’s stack frame from the stack and go to where the return address points a = 5 b = 3 a = 5 b = 3 no parameters Main’s Stack Frame return address return address intAdd(intnum1, int num2) { int sum; sum = num1 + num2; return sum, } no parameters no parameters Values returned from a method are passed in a special hardware register The Stack eax 8
control returns here static void Main() { int a = 5; int b = 3; int c = Add(a,b); Console.WriteLine("The answer is {0}", c); Console.ReadLine( ); } a = 5 b = 3 c = ? return address no parameters eax 8 The Stack
static void Main() { int a = 5; int b = 3; int c = add(a,b); Console.WriteLine("The answer is {0}", c); Console.ReadLine( ); } a = 5 b = 3 c = 8 8 return address no parameters The Stack
Pass By Value When a parameter is passed by value, a copy of the value is made and passed to the method on the run-time stack.
These names are local to the method Divide( ). The parameters passed to the method are given these names so that we can use them inside of the method. static double Divide(int n, int d) { double r = (double)n / d; n++; d++; return r; }
static void Main() { int num = 0, den = 0; do { Console.Write("Enter in an integer value: "); num = int.Parse(Console.ReadLine()); Console.Write("Enter in another integer value: "); den = int.Parse(Console.ReadLine()); if (den != 0) { double result = Divide(num, den); Console.WriteLine("{0}/{1} = {2}", num, den, result); } } while (den != 0); Console.ReadLine( ); }//End Main() num and den are called the actual parameters, or arguments.
let the value of num = 9 and the value of den = 7 return here if (den != 0) { Console.WriteLine("{0}/{1} = {2}", num, den, result); } num = 9 den = 7 double result = Divide (num, den); return address no parameters The Stack
r = 1.285 static double Divide(int n, int d) { double r = (double)n / d; n++; d++; return r; } return address d 8 10 7 9 n num = 9 den = 7 Notice that the original values in main’s stack frame don’t change. This is because n and d are names that are local to the Divide method. return address no parameters The Stack
Control now returns to the point where the function was called. In this case, the return value, in eax register, is then copied to “result”. 1.285 eax double result = Divide (num, den); num = 9 den = 7 result = 1.285 return address no parameters The Stack
Pass By Reference When a parameter is passed by reference, a reference to the value is made and passed to the method on the run-time stack.
the keyword refdenotes that this parameter is passed by reference! static double Divide(ref int n, ref int d) { double r = (double)n / d; n++; d++; return r; }
double result = Divide (ref num, ref den); Console.WriteLine("{0}/{1} = {2}", num, den, result); num = 9 den = 7 result = ? return address no parameters The Stack
Build the stack frame to call the divide method Return here when done executing the function return address ref to den ref to num double result = Divide (ref num, ref den); Console.WriteLine("{0}/{1} = {2}", num, den, result); num = 9 den = 7 result = ? return address no parameters The Stack
return address d ref to den ref to num static double Divide(ref int n, ref int d) { double r = (double)n / d; n++; d++; return r; } n num = 9 den = 7 result = ? return address no parameters The Stack
d return address n ref to den ref to num static double Divide(ref int n, ref int d) { double r = (double)n / d; n++; d++; return r; } num = 10 den = 8 These local variables, in main’s Stack frame, change, because d and n refer to them. result = ? return address no parameters The Stack
If you are passing simple data to a method, you should use pass-by-value Rule of Thumb avoids side effects!
If you need to change data in the calling method, for example swapping two values, then pass-by-reference. Objects are automatically passed by reference because it is more efficient. If a method has to return more than one value. When Should You Pass by Reference?
Example of Using a Side Effect Problem: Write a method that exchanges the values of two variables.
The exchange code ……… for exchanging integers value1 = value2; value2 = value1; int temp = value1; value1 = value2; value2 = temp;
Using pass by value … temp = 7 return address These are copies of num1 and num2 7 5 5 7 n2 n1 int num1 = 5; int num2 = 7; Swap (num1, num2); num1 = 5 num2 = 7 return address void Swap (int n1, int n2) { int temp = n1; n1 = n2; n2 = temp; } no parameters The Stack
Only the local variables allocated in Swap’s stack frame get swapped. The original values are not changed. To make the Swap work correctly, pass the parameters by reference.
Using pass by reference … temp = 7 return address These are references to num1 and num2 n2 n1 int num1 = 5; int num2 = 7; Swap (ref num1, ref num2); ref to num2 ref to num1 num1 = 5 num2 = 7 return address void Swap (ref intn1, ref intn2) { int temp = n1; n1 = n2; n2 = temp; } no parameters The Stack
Using pass by reference … temp = 7 return address ref to num2 ref to num1 n2 n1 int num1 = 5; int num2 = 7; Swap (ref num1, ref num2); num1 = 7 num2 = 5 So … the changes occur to num1 and num2 void Swap (ref intn1, ref intn2) { int temp = n1; n1 = n2; n2 = temp; } return address no parameters The Stack
Mixed Parameter Lists It is perfectly valid to mix pass-by-value and pass-by-reference parameters in the same method: void MethodTwo (ref intnum1, int num2);
Method Overloading In C# you can give two different methods the identical method name (but with different parameters) This is called method overloading. When a method is invoked, the compiler figures out which of the methods to use, based on the method name and the number, type and order of parameters.
this method has three parameters static int Max (int n1, int n2, int n3) { if ( n1 < n2 ) if ( n2 < n3 ) return n3; else return n2; else if ( n1 < n3 ) return n3; else return n1; } Example this method has two parameters static int Max (int n1, int n2) { if ( n1 < n2 ) return n2; else return n1; } int biggest = Max (5, 3); int largest = Max (5,3,7); this code will invoke this method this code will invoke this method
Method Signature A method’s signature refers to the method name and the number, sequence and type of parameters. A method is overloaded when the methods have the Same name but have different signatures.
Type Conversion and Overloading static double Mpg (double miles, double gallons) { return (miles / gallons); } if this method is called with the following code … int m = 15; int g = 3; double result = Mpg (m, g); m and g will be converted to double when they are passed to the method.
So … what happens if you also have this method in your program? intMpg (int goals, int misses) { return ( goals – misses); } and you make the method call int miles = 2; int gallons = 8; int result = Mpg (m,g); ?