Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 st Semester 2005 1 Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.

Similar presentations


Presentation on theme: "1 st Semester 2005 1 Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering."— Presentation transcript:

1 1 st Semester 2005 1 Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND

2 1 st Semester 2005 2/21 Outline  C# Method Review  Parameter Passing Pass by reference Pass by value

3 1 st Semester 2005 3/21 C# Structure – Multiple MethodsNamespace Class Main() Method1() Method2() Method3() MethodNN()

4 1 st Semester 2005 4/21 Method Types Method Type PredefinedMethodUser-definedMethod Returnvalue No return valueReturnvalue value

5 1 st Semester 2005 5/21 Method Types Method Type PredefinedMethodUser-definedMethod Returnvalue No return valueReturnvalue value

6 1 st Semester 2005 6/21 Method Declaration static ( ) { ; } #remark return-type can be - data type = int, double, string, … need return statement need return statement - void = return no value

7 1 st Semester 2005 7/21 Method Example: with return value … int x = power2(2); … static int power2(int n) { int j = n * n; return j; int j = n * n; return j;} *Methods can return only single value.

8 1 st Semester 2005 8/21 Method Example: without return value … DisplayName(”Aphirak”); … static void DisplayName(string s){ Console.WriteLine( ” Hello, {0}”,s); Console.WriteLine( ” Hello, {0}”,s);}

9 1 st Semester 2005 9/21 Method Example 1 0 1 2 3 4

10 1 st Semester 2005 10/21 Outline  C# Method Review  Passing Parameter Pass by value Pass by reference

11 1 st Semester 2005 11/21 Passing Parameter Example 1 … int x = power2(2); … static int power2(int n) { int j = n * n; return j; int j = n * n; return j;}

12 1 st Semester 2005 12/21 Passing Parameter Example 2 … int x = power2(2); … static int power2(int n) { n = n * n; return n; n = n * n; return n;}

13 1 st Semester 2005 13/21 Passing Parameter Type Passing Type Pass by value Pass by reference

14 1 st Semester 2005 14/21 Outline  C# Method Review  Passing Parameter Pass by value Pass by reference

15 1 st Semester 2005 15/21 Pass by value Example … string s; s = ”InitialD”; DisplayMovie(s); Console.WriteLine(s); … static void DisplayMovie(string st) { Console.WriteLine(”Movie = {0}”,st) Console.WriteLine(”Movie = {0}”,st) st = ”TomYumKung”; st = ”TomYumKung”;} Copy value s to st

16 1 st Semester 2005 16/21 Outline  C# Method Review  Passing Parameter Pass by value Pass by reference

17 1 st Semester 2005 17/21 Pass by reference Example … string s; s = ”InitialD”; DisplayMovie(ref s); Console.WriteLine(s); … static void DisplayMovie( ref string st) { Console.WriteLine(”Movie = {0}”,st) Console.WriteLine(”Movie = {0}”,st) st = ”TomYumKung”; st = ”TomYumKung”;} s is referred by st Changes st/changes s too

18 1 st Semester 2005 18/21 How to write method: swap x and y x = 5; y = 4; swap( x, y); Console.WriteLine(”x={0}, y={1}”,x,y); swap(ref x, ref y);

19 1 st Semester 2005 19/21 Keywords ref and out  C# also provides another pass by reference keyword: out  ref & out: static void Add(int a, int b, out int c) { c = a + b; } refout un-initialized variables are not allowed any variables are allowed previous values are passed to methods no previous values are passed to methods

20 1 st Semester 2005 20/21 Example  Min(x,y)  Find min value  Max(x, y)  Find max value  Mul(x, y)  Find x * y  IsPrime(x)  Is x a prime number?  CircleArea(r)  Find area or circle with radius r  BahtToDollar(b)  Convert baht to dollar

21 1 st Semester 2005 21/21 Summary  Method Return Value: int, double, etc… void  Passing Parameter Pass by value Pass by reference


Download ppt "1 st Semester 2005 1 Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering."

Similar presentations


Ads by Google