20 04 4111 111 Com puter and Com puter and 2 Program m ing Program m ing Lecture #6: M ethod O verloading, M ethod O verloading, Referenced Parameter Passing, Referenced Parameter Passing, Variable Scope and Duration, Variable Scope and Duration, Program Development Process, Program Development Process, and Dual Roles of Classes and Dual Roles of Classes http://mike.cpe.ku.ac.th/204111 TM approved M IKE Version 2006/2
Outline � Review � More about defining and using methods � Method header � Overloading and signature � Method parameter passing � Method body � Variable scope and duration � Program development process � Stepwise refinement using the Calendar program as an example � Dual roles of classes TM approved M IKE 2 Version 2006/2
Recap: Methods � A method should provide a well-defined, easy-to- understand functionality. � A method takes input (parameters), performs some actions, and (sometime) returns a value. � Writing a custom method � Header • Properties ReturnType MethodName( Pm1, Pm2, … ) � Body • Contains the code of what the method does. – Local variables declaration – Statements • Contains the return value if necessary. � All methods must be defined inside of a class. 3
Outline 1 // MaximumValue.cs 2 // Finding the maximum of three doubles. 3 MaximumValue.cs 4 using System; 5 6 class MaximumValue 7 { 8 // main entry point for application 9 static void Main( string[] args ) 10 { 11 // obtain user input and convert to double The program gets 3 12 Console.Write( "Enter first floating-point value: " ); values from the user 13 double number1 = Double.Parse( Console.ReadLine() ); 14 15 Console.Write( "Enter second floating-point value: " ); 16 double number2 = Double.Parse( Console.ReadLine() ); 17 18 Console.Write( "Enter third floating-point value: " ); 19 double number3 = Double.Parse( Console.ReadLine() ); 20 21 // call method Maximum to determine largest value 22 double max = Maximum( number1, number2, number3 ); 23 24 // display maximum value The three values are then passed 25 Console.WriteLine("\nmaximum is: " + max ); to the Maximum method for use 26 27 } // end method Main
Outline MaximumValue.cs 28 29 // Maximum method uses method Math.Max to help determine 30 // the maximum value 31 static double Maximum( double x, double y, double z ) 32 { 33 return Math.Max( x, Math.Max( y, z ) ); 34 35 } // end method Maximum 36 The Maximum method receives 3 37 } // end class MaximumValue variables and returns the largest one The use of Math.Max uses the Max method in class Math. The dot operator is used to call it. Enter first floating-point value: 37.3 Program Output Enter second floating-point value: 99.32 Enter third floating-point value: 27.1928 maximum is: 99.32
Method Overloading � The following lines use the WriteLine method for different data types: Console.WriteLine ("The total is:"); double total = 0; Console.WriteLine (total); � Method overloading is the process of using the same method name for multiple methods. � Usually perform the same task on different data types. � Example: The WriteLine method is overloaded: WriteLine (String s) WriteLine (int i) WriteLine (double d) … TM approved M IKE 6 Version 2006/2
Method Overloading: Signature � The compiler must be able to determine which version of the method is being invoked. � This is done by analyzing the parameters, which form the signature of a method � The signature includes the number, type, and order of the parameters. � The return type of the method is not part of the signature. TM approved M IKE 7 Version 2006/2
Method Overloading Version 1 Version 2 Version 1 Version 2 double TryMe (int x, double y) double TryMe (int x) { { return x*y; return x + .375; } } Invocation Invocation result = TryMe (25, 4.32) TM approved M IKE 8 Version 2006/2
Outline 1 // MethodOverload2.cs 2 // Overloaded methods with identical signatures and 3 // different return types. 4 MethodOverload2.cs 5 using System; 6 7 class MethodOverload2 8 { 9 static int Square( double x ) 10 { This method returns an integer 11 return x * x; 12 } 13 14 // second Square method takes same number, 15 // order and type of arguments, error 16 static double Square( double y ) 17 { 18 return y * y; This method returns a double number 19 } 20 21 // main entry point for application 22 static void Main() Since the compiler cannot tell 23 { which method to use based on 24 int squareValue = 2; 25 Square( squareValue ); passed values an error is generated 26 } 27 28 } // end of class MethodOverload2 Program Output
More Examples double TryMe ( int x ) TryMe( 1 ); { return x + 5; TryMe( 1.0 ); } TryMe( 1.0, 2); double TryMe ( double x ) { TryMe( 1, 2); return x * .375; } TryMe( 1.0, 2.0); double TryMe (double x, int y) { return x + y; } Click here 10
Recall: Calling a Method � Each time a method is called, the actual arguments in the invocation are copied into the formal arguments. Actual parameters int num = SquareSum (2, 3); static int SquareSum (int num1, int num2) { int sum = num1 + num2; return sum * sum; } Formal parameters TM approved M IKE 11 Version 2006/2
Parameters: Modifying Formal Arguments � You can use the formal arguments (parameters) as variables inside the method. � Question: If a formal argument is modified inside a method, will the actual argument be changed? static int Square ( int x ) { x = x * x; return x; } static void Main ( string[] args ) { int x = 8; int y = Square( x ); Console.WriteLine ( x ); } TM approved M IKE 12 Version 2006/2
Parameter Passing � Call by value: a modification on the formal argument has no effect on the actual argument. � Call by reference: a modification on the formal argument can change the actual argument. TM approved M IKE 13 Version 2006/2
Call-By-Value and Call-By-Reference in C# � Depend on the type of the formal argument. � For the simple data types, it is call-by-value. � Change to call-by-reference � The ref keyword and the out keyword change a parameter to call-by-reference. • If a formal argument is modified in a method, the value is changed. • The ref or out keyword is required in both method declaration and method call. • ref requires that the parameter be initialized before enter a called method, while out requires that the parameter be set before return from a called method. TM approved M IKE 14 Version 2006/2
Example: ref static void Foo( int p ) {++p;} static void Main ( string[] args ) { int x = 8; Foo( x ); // a copy of x is made Console.WriteLine( x ); } static void Foo( ref int p ) {++p;} static void Main ( string[] args ) { int x = 8; Foo( ref x ); // x is ref Console.WriteLine( x ); } See TestRef.cs TM approved M IKE 15 Version 2006/2
Example: out static void Split( int timeLate, out int days, out int hours, out minutes ) { days = timeLate / 10000; hours = (timeLate / 100) % 100; minutes = timeLate % 100; } static void Main ( string[] args ) { int d, h, m; Split( 12345, out d, out h, out m ); Console.WriteLine( “ {0}d {1}h {2}m ” , d, h, m ); } See TestOut.cs TM approved M IKE 16 Version 2006/2
Outline 1 // RefOutTest.cs 2 // Demonstrating ref and out parameters. 3 RefOutTest.cs 4 using System; When passing a value by reference 5 using System.Windows.Forms; the value will be altered in the rest 6 of the program as well 7 class RefOutTest { 8 // x is passed as a ref int (original value will change) 9 static void SquareRef( ref int x ) { Since the methods are void 10 x = x * x; they do not need a return value. 11 } 12 13 // original value can be changed and initialized 14 static void SquareOut( out int x ) { 15 x = 6; Since x is passed as out the variable 16 x = x * x; can then be initialed in the method 17 } 18 19 // x is passed by value (original value not changed) 20 static void Square( int x ) { Since not specified, this value is defaulted to being 21 x = x * x; passed by value. The value of x will not be changed 22 } elsewhere in the program because a duplicate of the 23 variable is created. 24 static void Main( string[] args ) { 25 // create a new integer value, set it to 5 26 int y = 5; 27 int z; // declare z, but do not initialize it 28
Recommend
More recommend