Download presentation
Presentation is loading. Please wait.
Published byAshlee Jordan Modified over 9 years ago
1
METHOD 2 Thanachat Thanomkulabut 1
2
2 Outline Type of Method No Returned value Returned value No Parameter Pass by value refout Pass by reference Parameter Passing Method
3
3 Outline Method Overview Using Method Parameter Passing in Method No Parameter Pass by value Pass by reference
4
Parameter Passing Tree 4 No Parameter Pass by value Pass by reference Parameter Passing
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 “Actual parameters” can be called “arguments”.
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 nameage "Mc"18 Hey Mc!! Monitor Your age is 18.
15
Example 1 (Extend Edition) 15
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 Receive 4 Parameter Numerator of Fraction1 Denominator of Fraction1 Numerator 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 2 5 3 4 815 < n1 d1 n2 d2 Fraction 1 Fraction 2
20
Parameter Passing Tree 20 Parameter Passing No Parameter Pass by value Pass by reference
21
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; } 21
22
Pass by Reference 22 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
23
Pass by Reference 23 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
24
Passing By Reference (ref) 24 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
25
Example1 25 y 10 x 5 x = 5, y = 10 Output n2 10 n1 5 temp 5 10 5 5 x = 10, y = 5
26
Self Test II 26 Write the method exponent( a, b ) Change a to a b Change b to b a Example Before x=2, y=3 Output After x=8, y=9
27
Self Test II 27
28
Self Test II 28 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
29
Self Test III 29 Write Method playgame Parameter Score of player Point of this game Method Duty Receive 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
30
Self Test III 30
31
Copy value s to st st ="SuperMan2" st ="SpiderMan" 31 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";} s = "SuperMan2" Movie = SuperMan2 SuperMan2
32
32 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 s =“SpiderMan" st =“SpiderMan"
33
“ref” disability 33 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
34
34 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**
35
“ref” disability But “out” can do it 35 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
36
Pass by reference (Out) 36 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
37
Self Test IV 37 Write the method ReadInfo Method Duty Read NAME and SCORE of user, send both back to calling method via parameter of method
38
Self Test V 38 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
39
Self Test VI 39 Write the method CalDisandSlope First parameter : x-coordinate of point 1 Second parameter : y-coordinate of point 1 Third parameter : x-coordinate of point 2 Fourth parameter : y-coordinate of point 2 Fifth parameter (out) : distance between point 1 & 2 Sixth parameter (out) : slope of strength line which pass point 1 & 2
40
ANY QUESTION? 40
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.