410 likes | 428 Views
Learn about passing parameters in C# methods, including pass by value and pass by reference. Explore examples and understand the implications of each method.
E N D
Method 2 Thanachat Thanomkulabut
Outline • Method Overview • Using Method • Parameter Passing in Method • No Parameter • Pass by value • Pass by reference
Parameter Passing in C# Parameter Passing Location static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; }
No Parameter using System; class NReturned { static void HelloM() { Console.WriteLine(”Hello, Mckazine”); } static void Main() { HelloM(); HelloM(); HelloM(); HelloM(); } }
static void Main(){ showtitle(); if(Is_Member() == true) Console.WriteLine(“Welcome my friend”); else Console.WriteLine("Sorry. You cannot admit"); } static void showtitle(){ Console.WriteLine("This is Mc Webboard"); Console.WriteLine("Create by Mckazine"); } static bool Is_Member(){ Console.Write("Are you member (Y/N)"); char member = char.Parse(Console.ReadLine()); if(member == 'Y') return true; else return false; }
Pass by value • Passing a copy of the variable to the method • Passing value can be both variable and logical expression • Change of variable in the method has no effect on the original
Parameter passing This is called "pass by value." The data value of size is copied to s. static void Main(){ ... PrintBox(size); ...} 'x' is copied to c. s-2 is evaluated and the resulting value is copied to n. static void PrintBox(int s){ ... PrintChar('x',s-2); ...} static void PrintChar(char c, int n){ ... }
Parameter passing size is an actual parameter. static void Main(){ ... PrintBox(size); ...} s is an formal parameter. static void PrintBox(int s){ ... PrintChar('x',s-2); ...} 'x' and s-2 are actual parameters. static void PrintChar(char c, int n){ ... } c and n are formal parameters
Pass by value • Passing value can be both variable and logical expression static void Subject (double num, bool okay, int unit) { ... ... ... } static void Main() { double n; int limit; bool found; ... ... Subject(12.7, true, (limit*3)+5); ... ... }
Pass by value Formal Parameter static void Subject (double num, bool okay, int unit) { ... ... ... } static void Main() { double n=2.5; int limit=0; bool found = true; ... ... ... ... Subject(n, found, limit); ... ... } Actual Parameter
Example 1 Monitor Hey Mc!! static void Main() { Showinfo(“Mc”, 18); } static void Showinfo(string name,int age){ Console.WriteLine(“Hey {0}!!”,name); Console.WriteLine(“Your age is {0}.”,age); } Your age is 18. name age “Mc” 18
Example 1 (Extend Edition) static void Main() { string username; int born_year; Console.Write("Input your name : "); username = Console.ReadLine(); Console.Write("What year did you born ? "); born_year = int.Parse(Console.ReadLine()); Showinfo(username, 2009-born_year); } static void Showinfo(string name,int age){ Console.WriteLine(“Hey {0}!!”,name); Console.WriteLine(“Your age is {0}.”,age); }
Pass by value n = 25 Num = 5 • Change of variable in the method has no effect on the original static void Square(int n) { n = n*n; Console.WriteLine("n = {0}",n); } n 25 5 static void Main() { int num=5; Square(num); Console.WriteLine("Num = {0}",num); } Num 5
Pass by value static void Main() { int x=5,y; y = Add10(x); } static int Add10(int x){ x = x+10; return x; } x y 5 15 15 x 5 15
Self Test I • Write Method comparefraction • Recieve 4 Parameter • Numarator of Fraction1 • Denominator of Fraction1 • Numarator of Fraction2 • Denominator of Fraction2 • Return • 1 if Fraction1 more than Fraction2 • 0 if Fraction1 equal to Fraction2 • -1 if Fraction1 less than Fraction2
Self Test I Fraction1 Fraction2 8 15 n1 n2 2 3 < d1 d2 5 4 static int compare_fraction (int n1,int d1,int n2,int d2){ if(n1*d2 > n2*d1) return 1; else if(n1*d2 < n2*d1) return -1; else return 0; }
Pass by value : Example static void Compute(int op1, int op2) { int i = 1, answer; answer = 1; while (i <= op2) { answer = op1*answer; i = i+1; } Console.WriteLine(answer); } static void Main() { int op1, op2; Console.WriteLine(“Enter 2 numbers”); op1 = int.Parse(Console.ReadLine()); op2 = int.Parse(Console.ReadLine()); Compute(op1, op2); }
Pass by reference • Suppose that we want to write a method that swap the values of two variables. • This method does not work. Why? static void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } static void Main() { ... int x,y; ... swap(x,y); }
Pass by Reference • Passing the variable's reference to the method • The formal parameter acts as a reference to the actual parameter. • Any changes to the formal parameter effects the actual parameter, since they refer to the same variable. • **use the ref or out keyword** • Passing by value, and passing by reference can be used together in same method
Pass by Reference static void subject(ref double num, ref bool okay, double total, int unit) { ... ... ... } num okay total unit 5A 6B 5.4 10 static void Main( ) { double n=6.2, sum=5.4; int limit=10; bool found=true; ... subject(ref n, ref found, sum, limit); ... } n found sum limit 5A 6B 6.2 TRUE 5.4 10
Output Passing By Reference (ref) Before calling = 50 In the method = 60 After calling = 60 reference tomyInt ispassed tox x 50 60 static void Add10(ref int x) { x += 10; Console.WriteLine("In the method = {0}",x); } static void Main() { int myInt = 50; Console.WriteLine("Before calling = {0}",myInt); Add10(ref myInt); Console.WriteLine("After calling = {0}",myInt); } SoxandmyInt are the same variables myInt 50 60
Output Example1 x = 5, y = 10 x = 10, y = 5 static void swap(ref int n1, ref int n2) { int temp = n1; n1 = n2; n2 = temp; } static void Main() { int x = 5,y = 10; Console.WriteLine(“x = {0}, y = {1}”,x,y); swap(ref x,ref y); Console.WriteLine(“x = {0}, y = {1}”,x,y); } n1 n2 temp 10 5 5 10 5 x y 10 5 10 5
Self Test II • Write the method exponent( a, b ) • Change a to ab • Change b to ba • Example Output Before x=2, y=3 After x=8, y=9 static void Main() { double x = 2,y = 3; Console.WriteLine(“Before x={0}, y={1}”,x,y); exponent(ref x,ref y); Console.WriteLine(“After x={0}, y={1}”,x,y); }
Self Test II static void Main() { double x = 2,y = 3; Console.WriteLine(“Before x={0}, y={1}”,x,y); exponent(ref x,ref y); Console.WriteLine(“After x={0}, y={1}”,x,y); } static void exponent(ref int a, ref int b) { double temp_a = a; temp_b = b; a = Math.Pow(temp_a,temp_b); b = Math.Pow(temp_b,temp_a); }
Self Test II Output Before x=2, y=3 x y After x=8, y=9 static void Main() { double x = 2,y = 3; Console.WriteLine(“Before x={0}, y={1}”,x,y); exponent(ref x,ref y); Console.WriteLine(“After x={0}, y={1}”,x,y); } 8 2 9 3 static void exponent(ref int a, ref int b) { double temp_a = a; temp_b = b; a = Math.Pow(temp_a,temp_b); b = Math.Pow(temp_b,temp_a); } temp_a temp_b a b 2 3 2 9 3 8
Self Test III • Write Method playgame • Parameter • Score of player • Point of this game • Method Duty • Recieve input “W” if player win, “L” if player lose • If player win, • Score of player will be added by point of this game • Return true • If player lose • Return false
Self Test III static bool playgame(ref int score,int point) { string result; Console.Write(“Are you win : ”); result = Console.ReadLine(); if(result == “W”){ score = score + point; return true; } else{ return false; } }
Copy value s to st Pass by value Example Movie = SuperMan2 SuperMan2 static void Main(){ string s; s = "SuperMan2"; DisplayMovie(s); Console.WriteLine(s); } s = "SuperMan2" st ="SuperMan2" st =“SpiderMan" static void DisplayMovie(string st) { Console.WriteLine("Movie = {0}",st); st = "SpiderMan"; }
s is referred by st Pass by reference Example Movie = SuperMan2 SpiderMan static void Main(){ string s; s = "SuperMan2"; DisplayMovie(refs); Console.WriteLine(s); } s = "SuperMan2" s =“SpiderMan" st ="SuperMan2" st =“SpiderMan" static void DisplayMovie(refstring st) { Console.WriteLine("Movie = {0}",st); st = "SpiderMan"; }
“ref” disability static void add(int a, ref int b, ref int c) { Console.WriteLine(c); c = a + b; } Error static void Main() { int a, b, c; a = 20; b = 10; add(a, ref b, ref c); } Cannot pass un-initialized variables
Passing By Reference ref and out ref and out Keywords **ref and out can be used in same method**
“ref” disability But “out” can do it static void add(int a, ref int b, ref int c) { c = a + b; } static void add(int a, ref int b, out int c) { c = a + b; } a b c Error 20 10 30 static void Main() { int a, b, c; a = 20; b = 10; add(a, ref b, out c); } static void Main() { int a, b, c; a = 20; b = 10; add(a, ref b, ref c); } a b c Cannot pass un-initialized variables 20 10 30
Ohiyo x = 4, y = 6, z = 10 Pass by reference (Out) Finally a = 2, b= 6, c = 10 static void Ohiyo(int x, ref int y, out int z) { x = 2*x; y = 2*y; z = x+y; Console.WriteLine(“Ohiyo x = {0}, y = {1}, z = {2}”, x,y,z); } x y z 10 4 2 6 3 static void Main() { int a, b, c; a = 2; b = 3; c = 4; Ohiyo(a, ref b, out c); Console.WriteLine(“Finally a = {0}, b = {1}, c = {2}”,a,b,c); } a b c 6 2 3 4 10
Self Test IV • Write the method ReadInfo • Method Duty • Read NAME and SCORE of user , send both back to calling method via parameter of method
Self Test V • Write the method DivideNumber • First parameter is dividend • Second parameter is divider • Third parameter is remainder of first and second parameter • Return quotient of first and second parameter
Self Test VI • Write the method CalDisandSlope • First parameter : x-coordinate of point 1 • Second parameter : y-coordinate of point 1 • Third parameter : x-coorddinate of point 2 • Fourth parameter : y-coordinate of point 2 • Fifth parameter (out) : distance between point 1 & 2 • Sixth parameter (out) : slope of strenght line which pass point 1 & 2