100 likes | 329 Views
6. Method. Computer Programming 1. Method. A method is a code block containing a series of statements. In C#, every executed instruction is done so in the context of a method ( subroutine, procedure or function ).
E N D
6. Method Computer Programming 1
Method Amethod is a code block containing a series of statements. In C#, every executed instruction is done so in the context of a method (subroutine, procedure or function). Methods are declared within a class or struct by specifying the access level, the return value, the name of the method, and any method parameters. Method parameters are surrounded by parentheses, and separated by commas. Empty parentheses indicate that the method requires no parameters. This class contains three methods
Ex. Method using System; namespace exMethod { public class exMethod { static void Main(string[] args) { // statement; } method name...1() { // statement; } method name...2() { // statement; } } }
Structure of Method • Format: Modifiers return_typemethod_name(parameter_List) { Statement; [return] } • Details: • Modifiers have / don’t have • Private to use in this class • Public to use every class • Return type • Void no return • Int, double, etc return in 1 value • Method name The name of methods • Parameter List send value in to method • Return send value back to main
Example 1 of Method using System; namespace exMethod { public class exMethod { static void Main() { WellcomeMessage(); Console.WriteLine("Hello"); Console.ReadLine(); } public static void WellcomeMessage() { Console.WriteLine("Wellcome"); Console.WriteLine("How you ready to methods !"); Console.WriteLine("555"); } } }
Example 2 of Method using System; namespace exMethod { public class exMethod { static void Main() { line(); Console.WriteLine("\nHello"); line(); } public static void line() { for (int i=0;i<10;i++) { Console.Write("*"); } } } }
Example 3 of Method pass value using System; namespace exMethod { public class exMethod { static void Main() { Console.Write("Enter Star : "); int num = Int32.Parse(Console.ReadLine()); printstar(num); } public static void printstar(int star) { for (inti=0;i<star;i++) { Console.Write("* "); } } } }
Example 4 of Method pass value using System; namespace exMethod { public class exMethod { static void Main() { Console.Write("Enter Start num : "); int startNum = Int32.Parse(Console.ReadLine()); Console.Write("Enter Stop num : "); int stopNum = Int32.Parse(Console.ReadLine()); printNumStartStop(startNum,stopNum); } public static void printNumStartStop(int start,int stop) { for (int i=start;i<=stop;i++) { Console.Write(i + " "); } } } }
Example 5 of Method Return value using System; namespace exMethod { public class exMethod { static void Main() { Console.Write("\nSum 4 + 5 = {0}",sumation(4,5)); Console.Write("\nSum 1 + 3 = {0}",sumation(1,3)); Console.Write("\nSum 9 + 7 = {0}",sumation(9,7)); } public static int sumation(int x,int y) { return x+y; } } }