Download presentation
Presentation is loading. Please wait.
Published byElfrieda Brooks Modified over 9 years ago
1
METHOD 2 Thanachat Thanomkulabut 1
2
2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass by reference refout
3
3 Outline Method Overview Using Method Parameter Passing in Method No Parameter Pass by value Pass by reference
4
Parameter Passing Tree 4 Parameter Passing No Parameter Pass by value Pass by reference
5
5 Parameter Passing in C# static ( ) { ; } Parameter Passing Location
6
No Parameter 6 using System; class NReturned { static void HelloM() { static void HelloM() { Console.WriteLine(”Hello, Mckazine”); Console.WriteLine(”Hello, Mckazine”); } static void Main() { HelloM(); HelloM(); }
7
7 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; }
8
Parameter Passing Tree 8 Parameter Passing No Parameter Pass by value Pass by reference
9
Pass by value 9 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
10
Parameter passing static void Main(){... PrintBox(size);... } This is called "pass by value." static void PrintChar(char c, int n){... } static void PrintBox(int s){... PrintChar('x',s-2);... } The data value of size is copied to s. 'x' is copied to c. s-2 is evaluated and the resulting value is copied to n. 10
11
Parameter passing static void Main(){... PrintBox(size);... } static void PrintChar(char c, int n){... } static void PrintBox(int s){... PrintChar('x',s-2);... } size is an actual parameter. s is an formal parameter. 'x' and s-2 are actual parameters. c and n are formal parameters 11
12
Pass by value 12 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);...... }
13
Pass by value 13 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 Formal Parameter
14
Example 1 14 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); } nameage “Mc”18 Hey Mc!! Monitor Your age is 18.
15
Example 1 (Extend Edition) 15 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); }
16
Pass by value 16 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); } static void Main() { int num=5; Square(num); Console.WriteLine("Num = {0}",num); } 5 Num n 525 n = 25 Num = 5
17
Pass by value 17 static void Main() { int x=5,y; y = Add10(x); } static int Add10(int x){ x = x+10; return x; } 5 xy x 515
18
Self Test I 18 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
19
Self Test I 19 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; } 2 5 3 4 815 < n1 d1 n2 d2 Fraction1Fraction2
20
Pass by value : Example 20 static void Main() { int op1, op2; Console.WriteLine(“Enter 2 numbers”); op1 = int.Parse(Console.ReadLine()); op2 = int.Parse(Console.ReadLine()); Compute(op1, op2); } 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); }
21
Parameter Passing Tree 21 Parameter Passing No Parameter Pass by value Pass by reference
22
Suppose that we want to write a method that swap the values of two variables. This method does not work. Why? static void Main() {... int x,y;... swap(x,y); } static void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } 22
23
Pass by Reference 23 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
24
Pass by Reference 24 static void subject(ref double num, ref bool okay, double total, int unit) {... } static void Main( ) { double n=6.2, sum=5.4; int limit=10; bool found=true;... subject(ref n, ref found, sum, limit);... } nfoundsumlimit numokaytotalunit 5A 6B 6.2 TRUE5.410 5A 6B5.410
25
Passing By Reference ( ref ) 25 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); } reference to myInt is passed to x So x and myInt are the same variables myInt 50 Before calling = 50 Output x 5060 In the method = 60 After calling = 60
26
Example1 26 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); } y 10 x 5 x = 5, y = 10 Output n2 10 n1 5 temp 5 10 5 5 x = 10, y = 5
27
Self Test II 27 Write the method exponent( a, b ) Change a to a b Change b to b a Example 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); } Before x=2, y=3 Output After x=8, y=9
28
Self Test II 28 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); }
29
Self Test II 29 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); } x 2 y 3 Before x=2, y=3 Output a 2 b 3 temp_a 2 temp_b 3 8 8 9 9 After x=8, y=9
30
Self Test III 30 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
31
Self Test III 31 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; }
32
32 Pass by value Example static void Main(){ string s; string s; s = "SuperMan2"; s = "SuperMan2"; DisplayMovie(s); DisplayMovie(s); Console.WriteLine(s); Console.WriteLine(s);} static void DisplayMovie(string st) { Console.WriteLine("Movie = {0}",st); Console.WriteLine("Movie = {0}",st); st = "SpiderMan"; st = "SpiderMan";} Copy value s to st s = "SuperMan2" Movie = SuperMan2 st ="SuperMan2" SuperMan2 st =“SpiderMan"
33
33 Pass by reference Example static void Main(){ string s; string s; s = "SuperMan2"; s = "SuperMan2"; DisplayMovie(ref s); DisplayMovie(ref s); Console.WriteLine(s); Console.WriteLine(s);} static void DisplayMovie(ref string st) { Console.WriteLine("Movie = {0}",st); Console.WriteLine("Movie = {0}",st); st = "SpiderMan"; st = "SpiderMan";} s is referred by st s = "SuperMan2" Movie = SuperMan2 st ="SuperMan2" SpiderMan st =“SpiderMan" s =“SpiderMan"
34
“ref” disability 34 static void add(int a, ref int b, ref int c) { Console.WriteLine(c); c = a + b; } static void Main() { int a, b, c; a = 20; b = 10; add(a, ref b, ref c); } Error Cannot pass un-initialized variables
35
35 refout Passing By Reference ref and out refout un-initialized variables are not allowed any variables are allowed values are passed to methods via parameter no values are passed to methods via parameter refout ref and out Keywords refout ** ref and out can be used in same method**
36
“ref” disability But “out” can do it 36 static void add(int a, ref int b, ref int c) { c = a + b; } static void Main() { int a, b, c; a = 20; b = 10; add(a, ref b, ref c); } Error Cannot pass un-initialized variables static void Main() { int a, b, c; a = 20; b = 10; add(a, ref b, out c); } static void add(int a, ref int b, out int c) { c = a + b; } a 20 b 10 c 30 a 20 b 10 c 30
37
Pass by reference (Out) 37 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); } 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); } a 2 b 3 c 4 x 2 y 3 z 10 46 Ohiyo x = 4, y = 6, z = 10 Finally a = 2, b= 6, c = 10 6
38
Self Test IV 38 Write the method ReadInfo Method Duty Read NAME and SCORE of user, send both back to calling method via parameter of method
39
Self Test V 39 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
40
Self Test VI 40 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
41
ANY QUESTION? 41
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.