1 / 9

6. Method

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

pennie
Download Presentation

6. Method

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 6. Method Computer Programming 1

  2. 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

  3. Ex. Method using System; namespace exMethod { public class exMethod { static void Main(string[] args) { // statement; } method name...1() { // statement; } method name...2() { // statement; } } }

  4. 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

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

  6. 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("*"); } } } }

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

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

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

More Related