360 likes | 364 Views
Method 1. Thanachat Thanomkulabut. Outline. Method Idea. Password. Input1. Input2. Light Switch. Safe. Calculator. Output. Pre-definded Method in C#. Console.WriteLine(”Hello world!”);. double j = Math.Sqrt(9);. Hello world!. 9. Console.WriteLine. Math.Sqrt. 3.
E N D
Method 1 Thanachat Thanomkulabut
Method Idea Password Input1 Input2 Light Switch Safe Calculator Output
Pre-definded Method in C# • Console.WriteLine(”Hello world!”); • double j = Math.Sqrt(9); Hello world! 9 Console.WriteLine Math.Sqrt 3
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
What is Method? Method is a function/procedure owned by class. System.Console.WriteLine(“Hello World!”);
Type of Method 1. Non-Returned Value Method - Procedure Console.WriteLine(“Hello World!”); Parameter Method Name Class Name 2. Returned Value Method – Function • J = Math.Max(5, 20); 20 Class Name Parameter Method Name
Type of Method n1 n2 Hello world! Math.Max Console.WriteLine Max of n1 & n2 Return no value(void) Return Value
Method Example *** Program End using System; class method_example { static voidMain() { displaybox(); Console.WriteLine("Program End"); } static voiddisplaybox() { Console.WriteLine("x"); Console.WriteLine("xx"); } } Monitor 0 1 3 2
Outline • Method Overview • Using Method • Parameter Passing in Method • Pass by value • Pass by reference
Using User-Defined Method 1.DefineMethod 2.Call Method
Define Method in C# static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; } • #remarkreturn-type • return type can beint, double, string, … • and need return statement • void= return no value
Method Declaration Example staticvoid displaybox () { } staticint calage () { } staticint calbox(int x,int y) { }
Example1: Non-Returned Value using System; class NReturned { static void Main() { Console.WriteLine(”Hello, Mckazine”); Console.WriteLine(”Hello, Mckazine”); Console.WriteLine(”Hello, Mckazine”); Console.WriteLine(”Hello, Mckazine”); } } HelloM Hello, Mckazine
Example1: Non-Returned Value using System; class NReturned { static void HelloM() { Console.WriteLine(”Hello, Mckazine”); } static void Main() { HelloM(); HelloM(); HelloM(); HelloM(); } } Hello Mckazine Hello Mckazine Hello Mckazine Hello Mckazine
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
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”); } } string name Hi Monitor Hi,AAA Hi, name Hi, BBB Hi, CCC
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(); Console.WriteLine("Square size = 4"); for(i=0;i<4;i++){ for(j=0;j<4;j++) Console.Write("*"); Console.WriteLine(); } Console.WriteLine(); } int n DrawSquare Result ... **** * **** * **** * **** * ... Square size = 2** ** Square size = 4**** **** **** ... ...
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(); } Console.WriteLine(); } Monitor int n Square size = 2 **** DrawSquare Square size = 4 ... ************ **** **** * **** * **** * **** * ... ... ...
Self Test 1 • Write the method PrintNumber(n) • Recieve parameter n • Show 1234...n Example int n 7 PrintNumber PrintNumber 1234...n 1234567
Self Test 1 int n PrintNumber • Write the method PrintNumber(n) • Recieve parameter n • Show 1234...n 1234...n static void PrintNumber(int n) { int i; for(i=1;i<=n;i++) Console.Write(i); }
Define Method in C# static <return-type> <method-name>(<parameter list>) { <const/variable declaration>; <statements>; } • #remarkreturn-type • return type can beint, double, string, … • and need return statement • void= return no value
Return value method n 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
Example 1: Returned Value using System; class Returned { static void Main() { int j; j = 2*2; Console.WriteLine(”{0}”, j); j = 5*5; Console.WriteLine(”{0}”, j); } } n Power2 n2
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; Console.WriteLine(”{0}”, j); } } n Power2 n2 Result 4 25
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.
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
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); } x Is_Prime True if n is prime.False if n is not prime
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); } h r V_Cylinder V
Example 3: Returned Value 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); } h r V_Cylinder V
Self Test 1 • Define Power4(n) Method • 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;
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) ans = x; return x; else if(y >= x && y >= z) return y; ans = y; else ans = z; return z; return ans;
Main() Namespace Class Method1() Method2() Method3() MethodN() C# Structure – Multiple Methods
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); }