Functions Used to write code only once Can use parameters
using System; namespace Demo01 { class Program static void Main(string[] args) string name = "world"; int year = 2017; for (int i = 0; i < 10; i++) PrintYear(name, i + year); } private static void PrintYear(string name, int year) Console.WriteLine("Hello " + name + ", the year is " + year);
string name = "Loek"; Console.WriteLine(name); name = "Antje"; int year = 2017; for (int i = 0; i < 10; i++) { // everything that happens here, happens 10 times Console.WriteLine("Hello " + name + " the current year is " + (year + i)); }
Condition
int startYear = 2010; for (int i = 0; i < 10; i++) { int year = startYear + i; if (year == 2017) Console.WriteLine(year + " is the current year"); } else Console.WriteLine(year + " is NOT the current year");
int startYear = 2010; for (int i = 0; i < 10; i++) { int year = startYear + i; if (year == 2017) Console.WriteLine(year + " is the current year"); } else if (year < 2017) Console.WriteLine(year + " is before the current year"); else Console.WriteLine(year + " is after the current year");
int startYear = 2010; for (int i = 2010; i < 2020; i++) { int year = startYear + i; if (year == 2017) Console.WriteLine(year + " is the current year"); } else if (year < 2017) Console.WriteLine(year + " is before the current year"); else Console.WriteLine(year + " is after the current year");
Methods
Console.WriteLine("+======================================+"); Console.WriteLine("Hello, my name is: LOEK"); Console.WriteLine(""); Console.WriteLine("Hello, my name is: LEO"); Console.WriteLine("Hello, my name is: PIET");
static void Main(string[] args) { Console.WriteLine("Atendee list"); Console.WriteLine("––––––––––––––––––––––"); Console.WriteLine(""); PrintName("Karl"); PrintName("Petra"); } static void PrintName(string name) Console.WriteLine(name);