41
METHOD 2 Thanachat Thanomkulabut 1

METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Embed Size (px)

Citation preview

Page 1: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

METHOD 2Thanachat Thanomkulabut

1

Page 2: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

2

Outline

Page 3: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

3

Outline

Method Overview Using Method Parameter Passing in Method

No Parameter Pass by value Pass by reference

Page 4: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Parameter Passing Tree4

Page 5: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

5

Parameter Passing in C#

static <return-type> <method-name>(<<parameter listparameter list>>){ <const/variable declaration>; <statements>;}

Parameter Passing LocationParameter Passing Location

Page 6: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

No Parameter6

using System;class NReturned {

static void Hellostatic void HelloMM() {() {

Console.WriteLine(”Hello, Console.WriteLine(”Hello, Mckazine”Mckazine”););

}} static void Main() {

HelloHelloMM();();

HelloHelloMM();();

HelloHelloMM();();

HelloHelloMM();(); }}

Page 7: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

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;}

Page 8: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Parameter Passing Tree8

Page 9: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Pass by value9

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

Page 10: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

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 resultingvalue is copied to n.

10

Page 11: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

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

Page 12: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Pass by value12

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); ... ...}

Page 13: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Pass by value13

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

Page 14: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Example 114

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);}

name age

“Mc” 18

Hey Mc!!Monitor

Your age is 18.

Page 15: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

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);}

Page 16: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Pass by value16

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 = 25Num = 5

Page 17: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Pass by value17

static void Main() { int x=5,y; y = Add10(x);}

static int Add10(int x){ x = x+10; return x;}

5

x y

x

515

15 15

Page 18: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test I18

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

Page 19: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test I19

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;}

25

34

8 15< n1

d1n2d2

Fraction1

Fraction2

Page 20: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Pass by value : Example20

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);}

Page 21: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Parameter Passing Tree21

Page 22: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

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 Main(){ ... int x,y; ... swap(x,y);}

static void swap(int a, int b){ int temp; temp = a; a = b; b = temp;}

22

Page 23: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Pass by Reference23

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

Page 24: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Pass by Reference24

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); ...} n found sum limit

num okay total unit

5A 6B

6.2 TRUE

5.4 10

5A 6B 5.4 10

Page 25: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

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

60

In the method = 60After calling = 60

Page 26: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Example126

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

10

5

5

x = 10, y = 5

Page 27: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test II27

Write the method exponent( a, b ) Change a to ab

Change b to ba

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

Page 28: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test II28

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);}

Page 29: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test II29

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_a2

temp_b3 8

8

9

9After x=8, y=9

Page 30: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test III30

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

Page 31: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test III31

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; }}

Page 32: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

32

Pass by value Example

static static void void MainMain(){(){ string string ss;; s s = = "SuperMan2""SuperMan2";; DisplayMovieDisplayMovie((ss);); ConsoleConsole..WriteLineWriteLine((ss); ); }}static static void void DisplayMovieDisplayMovie((string string stst) {) { ConsoleConsole..WriteLineWriteLine(("Movie = {0}""Movie = {0}",,stst);); st st = = "SpiderMan""SpiderMan";;}}

Copy value Copy value ss to to stst

s s = = "SuperMan2""SuperMan2"

Movie = SuperMan2Movie = SuperMan2

st =st ="SuperMan2""SuperMan2"

SuperMan2SuperMan2

st =st =“SpiderMan"“SpiderMan"

Page 33: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

33

Pass by reference Example

static static void void MainMain(){(){ string string ss;; s s = = "SuperMan2""SuperMan2";; DisplayMovieDisplayMovie((refref ss);); ConsoleConsole..WriteLineWriteLine((ss); ); }}

static static void void DisplayMovieDisplayMovie((refref string string stst) {) { ConsoleConsole..WriteLineWriteLine(("Movie = {0}""Movie = {0}",,stst);); st st = = "SpiderMan""SpiderMan";;}}

ss is referred by is referred by stst

s s = = "SuperMan2""SuperMan2"

Movie = SuperMan2Movie = SuperMan2

st =st ="SuperMan2""SuperMan2"

SpiderManSpiderMan

st =st =“SpiderMan"“SpiderMan"

s =s =“SpiderMan"“SpiderMan"

Page 34: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

“ref” disability34

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

Page 35: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

35

Passing By Reference refref and outout

refref outout

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

refref and outout Keywords

**refref and outout can be used in same method**

Page 36: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

“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

Page 37: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

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

104 6

10

Ohiyo x = 4, y = 6, z = Ohiyo x = 4, y = 6, z = 1010Finally a = 2, b= 6, c = Finally a = 2, b= 6, c = 1010

6

Page 38: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test IV38

Write the method ReadInfo Method Duty

Read NAME and SCORE of user , send both back to calling method via parameter of method

Page 39: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test V39

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

Page 40: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

Self Test VI40

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

Page 41: METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass

ANY QUESTION?

41