Presentation is loading. Please wait.

Presentation is loading. Please wait.

Thanachat Thanomkulabut

Similar presentations


Presentation on theme: "Thanachat Thanomkulabut"— Presentation transcript:

1 Thanachat Thanomkulabut
Method 1 Thanachat Thanomkulabut

2 Outline Method Type of Method No Returned value Returned value
Parameter Passing No Parameter Pass by value Pass by reference ref out

3 Method Idea Password Input1 Input2 Light Switch Safe Calculator Output

4 Pre-definded Method in C#
Console.WriteLine(” Hello world!”); double j = Math.Sqrt(9); Hello world! 9 Console.WriteLine Math.Sqrt 3

5 Pre-definded Method in C#
int N = int.Parse(”5”); double ans =Math.Max(9, 2); String “5” 9 2 int.Parse Math.Max Int 5 9

6 a function/procedure owned by class.
What is Method? Method is a function/procedure owned by class. System.Console.WriteLine(“Hello World!”);

7 Type of Method J = Math.Max(5, 20);
1. Non-Returned Value Method - Procedure Console.WriteLine(“Hello World!”); Method Name Parameter Class Name 2. Returned Value Method – Function J = Math.Max(5, 20); 20 Class Name Parameter Method Name

8 Type of Method n1 n2 Hello world! Math.Max Console. WriteLine
Max of n1 & n2 Return no value (void) Return Value

9 Method Example * ** Program End using System; class method_example { static void Main() { displaybox(); Console.WriteLine("Program End"); } static void displaybox() { Console.WriteLine("x"); Console.WriteLine("xx"); Monitor 1 3 2

10 Outline Method Overview Using Method Parameter Passing in Method
Pass by value Pass by reference

11 Using User-Defined Method
1.Define Method 2.Call Method

12 Define Method in C# #remark return-type
static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; } #remark return-type return type can be int, double, string, … and need return statement void = return no value

13 Method Declaration Example
static void displaybox () { } static int calage () { } static int calbox(int x,int y) { }

14 Example1: Non-Returned Value
using System; class NReturned { static void Main() { Console.WriteLine(”Hello, Mckazine”); } HelloM Hello, Mckazine

15 Example1: Non-Returned Value
using System; class NReturned { static void HelloM() { Console.WriteLine(”Hello, Mckazine”); } static void Main() { HelloM(); Hello Mckazine

16 Example2: Non-Returned Value with parameters
using System; class NReturned { static void Main() { Console.WriteLine(”Hi, AAA”); Console.WriteLine(”Hi, BBB”); Console.WriteLine(”Hi, CCC”); } string string name Hi Hi, name

17 Example2: Non-Returned Value with parameters
using System; class NReturned { static void Hi(string name) { Console.WriteLine(”Hi, {0}”, name); } static void Main() { Hi(”AAA”); Hi(”BBB”); Hi(”CCC”); Hi string name Hi, name Monitor Hi, AAA Hi, BBB Hi, CCC

18 **** * **** * **** * **** *
Example 3 static void Main(){ int i,j,n; Console.WriteLine("Square size = 2"); for(i=0;i<2;i++){ for(j=0;j<2;j++) Console.Write("*"); Console.WriteLine(); } Console.WriteLine("Square size = 4"); for(i=0;i<4;i++){ for(j=0;j<4;j++) int n DrawSquare Result ... **** * **** * **** * **** * ... Square size = 2 ** ** Square size = 4 **** **** ... ...

19 **** * **** * **** * **** *
Example 3 static void Main(){ Console.WriteLine("Square size = 2"); drawSquare(2); Console.WriteLine("Square size = 4"); drawSquare(4); } static void drawSquare(int n){ int i,j; for(i=0;i<n;i++){ for(j=0;j<n;j++) Console.Write("*"); Console.WriteLine(); Monitor DrawSquare int n **** * **** * **** * **** * ... Square size = 2 ** ** Square size = 4 **** **** **** ****

20 Self Test 1 Write the method PrintNumber(n) Example PrintNumber
Recieve parameter n Show n Example int n 7 PrintNumber PrintNumber n

21 Self Test 1 static void PrintNumber(int n) { int i;
Write the method PrintNumber(n) Recieve parameter n Show n n static void PrintNumber(int n) { int i; for(i=1;i<=n;i++) Console.Write(i); }

22 Outline Method Type of Method No Returned value Returned value
Parameter Passing No Parameter Pass by value Pass by reference ref out

23 Define Method in C# #remark return-type
static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; } #remark return-type return type can be int, double, string, … and need return statement void = return no value

24 Return value method n n n ans = Math.Sqrt(n); Math.Sqrt
static void PrintNumber(int n) { double n, ans; n = int.Parse(Console.ReadLine()); ans = Math.Sqrt(n); Console.Write(“Root {0} is equal {1}",n,ans); } Math.Sqrt n n

25 Example 1: Returned Value
using System; class Returned { static void Main() { int j; j = 2*2; Console.WriteLine(”{0}”, j); j = 5*5; } n Power2 n2

26 Example 1: Returned Value
using System; class Returned { static int Power2(int n) { return n*n; } static void Main() { int j; j = Power2(2); //j=2*2; Console.WriteLine(”{0}”, j); j = Power2(5); //j=5*5; Power2 n n2 Result 4 25

27 Example 2: Returned Value
static void Main() { int n,i,count=0; n = int.Parse(Console.ReadLine()); for(i=1;i<=n;i++) if(n%i==0) count++; if(count==2) Console.Write("{0} is Prime.",n); } Process for check Prime number.

28 Example 2: Returned Value
static void Main() { int n,i,count=0; n = int.Parse(Console.ReadLine()); for(i=1;i<=n;i++) if(n%i==0) count++; if(count==2) Console.Write("{0} is Prime.",n); } x Is_Prime True if x is prime. False if x is not prime

29 Example 2: Returned Value
static bool Is_Prime(int x){ int i,count=0; for(i=1;i<=x;i++) if(x%i==0) count++; if(count==2) return true; else return false; } static void Main(){ int n = int.Parse(Console.ReadLine()); if(Is_Prime(n) == true) Console.Write("{0} is Prime.",n); Is_Prime x True if n is prime. False if n is not prime

30 Example 3: Returned Value
static void Main() { double r,h,V; r = double.Parse(Console.ReadLine()); h = double.Parse(Console.ReadLine()); V = Math.Pi*r*r*h; Console.Write("V = {0}",V); } r h V_Cylinder V

31 Example 3: Returned Value
V_Cylinder r V h static double V_Cylinder(double r, double h){ return Math.PI*r*r*h; } static void Main() { double r,h,V; r = double.Parse(Console.ReadLine()); h = double.Parse(Console.ReadLine()); V = V_Cylinder(r,h); Console.Write("V = {0}",V);

32 Self Test 1 Define Power4(n) Method Ex. int j = Power4(3); //Return 81
Return n*n*n*n Ex. int j = Power4(3); //Return 81 int int n static ____ Power4(___________________) { } int ans; return n*n*n*n; ans = n*n*n*n; reture ans;

33 Self Test 2 Define Max(x, y, z) Method
Return Max Value Ex. int j = Max(5, 20, 12); //Return 20 int int x, int y, int z static ____ MAX(___________________) { } int ans; if(x >= y && x >= z) return x; ans = x; else if(y >= x && y >= z) ans = y; return y; else return z; ans = z; return ans;

34 C# Structure – Multiple Methods
Namespace Class Main() Method1() Method2() Method3() MethodN()

35 Example 5 : Mutiple Method
static double FindMedian(double n1, double n2) { return (n1+n2)/2; } static void Report(double x, double y) Console.Write("Median = {0} ", FindMedian(x,y)); static void Main(string[] args) { Console.Write("Please Key 2 Input : "); double a1 = double.Parse(Console.ReadLine()); double a2 = double.Parse(Console.ReadLine()); Report(a1, a2); }

36 Any question?


Download ppt "Thanachat Thanomkulabut"

Similar presentations


Ads by Google