Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

C# Function Basics

Читайте также:
  1. A PRIVATE FUNCTION
  2. Anonymous function conversions
  3. Anonymous function expressions
  4. Anonymous functions
  5. Asya______karmano (function)
  6. Attitudinal Function
  7. Basic approaches to language investigation. The functions of language.

Lesson 6: User-defined functions

User-defined functions are routines that can take parameters, perform calculations or other actions, and return a result.

C# Code that must be executed many times is a good candidate for functions. For Example, if you have to perform some operation that required you to cut and paste the same code over and over again can get pretty tedious. Also, C# function can provide a centralized location for common information. This way if you have to change common code you do not have to search and replace text in your code, you can just change the code in the function.

Lets see a programming example: The following example will find out which number is bigger or if the numbers are equal.

using System;

class Program

{

static void Main(string[] args)

{

int x, y;

x = 8; y = 1282;

if (x < y)

Console.WriteLine(y + " is bigger");

else if (x > y)

Console.WriteLine(x + " is bigger");

else

Console.WriteLine(x + " and " + y + " are equal");

////////////////Prints 1282 is bigger//////////////

x = 90; y = 34;

if (x < y)

Console.WriteLine(y + " is bigger");

else if (x > y)

Console.WriteLine(x + " is bigger");

else

Console.WriteLine(x + " and " + y + " are equal");

////////////////Prints 90 is bigger/////////////////

x = 1029465; y = -1283;

if (x < y)

Console.WriteLine(y + " is bigger");

else if (x > y)

Console.WriteLine(x + " is bigger");

else

Console.WriteLine(x + " and " + y + " are equal");

////////////////Prints 1029465 is bigger/////////////

x = 34; y = 34;

if (x < y)

Console.WriteLine(y + " is bigger");

else if (x > y)

Console.WriteLine(x + " is bigger");

else

Console.WriteLine(x + " and " + y + " are equal");

////////////////Prints 34 and 34 are equal///////////

Console.ReadLine();

}

}

Output:

1282 is bigger90 is bigger1029465 is bigger34 and 34 are equal

As you can see the if/else if statements are copied and pasted each time x and y changed. There is a more effiecient way to do this. Just image if that common code were hundreds of lines and you had to edit some part of that common code. We will you to C# functions in the next section

C# Function Basics

To define a C# function you must declare it outside the main() block because the definition cannot be executed, it is merely a definition.

1. using System;2. class Program3. {4. static void print() //Function declared outside of main block 5. {6. Console.WriteLine("In function");7. }8. static void Main() //Main Block 9. {10. print(); //Call print function 11. Console.Read();12. }13. }

 

Output:

In Function

· Static Keyword: We will not cover static in this section. We will cover it in the classes section. Just remember it must be included in your functions.

· void Keyword: void means that the function will not return a value

· Line 10: This is the syntax to run a function. Type the functions name followed by ();


Дата добавления: 2015-11-14; просмотров: 82 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Eacute;tude du texte| The C# out Parameters

mybiblioteka.su - 2015-2024 год. (0.005 сек.)