What are methods in c

Basics part 2 - own methods

We already got to know the WriteLine () method in the first section, but at that time we simply accepted that this method outputs a text to the console. Shortly afterwards we dealt with the ReadLine () method, which, in contrast, returned a text.
In general terms, is a Method a piece of program codethat called upon request can be.
This is especially useful when something is needed more often. Let us imagine a program in which several specific lines are to be output frequently. However, it is not possible to solve this with a loop, since it should not happen one after the other, but at different points in the program code.
The basic goal of methods is to make program code reusable.

Before we go into how to program our own method, we'll do that again Look at codethat of Visual c # when creating a new project is produced.

using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace project1
{
classProgram
      {
staticvoid Main (string [] args)
            {

            }
      }
}

Back then we did static void Main (string [] args) simply accepted, basically nothing other than a method is hidden behind it. The actual The name of the method is Main. We will hear later what the terms static, void and the string data field (array) in the round brackets mean. The Main method however has one Specialty. As it is the method that called at a program start (upper and lower case is relevant).
It is obvious, for the sake of completeness, that the Main method consists of the code between the curly braces. Let us now consider the simplest case of a method.

Methods without parameters and return value

With this type will no variable passed to the methodas it happens with WriteLine (string), for example. There are also no return value as with ReadLine (), which has a string as return value (ReadLine () represents a string).
A Method without parameters so, in simple terms, is a method without variables between the round brackets. With the Keyword void in front of the name of the method, it is indicated that this Method does not have a return value owns. Translated into German, void means nothing other than empty.
It should be noted that a Method must never be defined in another method.
Methods are always to be defined in the curly brackets of class Project1 (Project1 can vary). We will only deal with class Project1 in the next section.

A program with a method without parameters or return value, which has the name myMethod, would look like this:

using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace project1
{
classProgram
      {
staticvoid Main (string [] args)
            {

            }

staticvoid myMethod ()
            {
Code;
            }
      }
}

Here is again static at the beginning of the method definition, this is necessary for our future examples in this section, since Main is also static, but we shouldn't care about this at the moment. A call to one Method without parameters behaves similarly to that of Console.ReadLine (), however it is here no need to write a class name (console) in front of it.

Methods are called with the following program line:

If this line is in the Main method written will, will Code executed which is programmed in the method myMethode.

Exercise example

To improve understanding it is now the task to write a program with two methods without parameters or return values ​​and to call them to execute the code contained therein.

specification

  • Define the method textAusgabe1, in which the following text is to be output in the console: "Output 1"
  • Define the method textAusgabe2, in which the following text is to be output in the console: "Output 2"
  • Calling the method textAusgabe2 in Main
  • Calling the method textAusagbe1 in Main

Click here for the solution. If you have any questions or problems, please use the forum.

Solution:
using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace project1
{
classProgram
      {
staticvoid Main (string [] args)
            {
textAusgabe2 ();
textAusgabe1 ();
Console.ReadLine ();
            }

staticvoid textAusgabe1 ()
            {
Console.WriteLine ("Issue 1");
            }

staticvoid textAusgabe2 ()
            {
Console.WriteLine ("Issue 2");
            }
      }
}

Methods with parameters

The example has already been given here WriteLine () called there pass a string to this method which is output in the console.So we already know how to call a methodwhich parameter owns.
However, not how a method of this type is defined.
It's pretty easy to do. To do this, a Variable declared between the brackets when defining the methods(Are defined). A example it would be if a string should be passed to the method.
One writes about it string variable name between the round brackets the method. Then this variable absolutely normal use in the curly brackets of the method. To show a simple example, a program follows that calls a method and transfers a string to it, which is output by the method of the console.

using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace project1
{
classProgram
      {
staticvoid Main (string [] args)
            {
myMethod ("Text that should be displayed in the console!");
Console.ReadLine ();
            }

staticvoid myMethod (string output)
            {
Console.WriteLine (output);
            }
      }
}

Instead of the string variable, an int variable could of course also be passed in order to calculate and output something with it.

Something we haven't had before is the possibility pass more than one variable to a method. It is to this It is necessary to declare several variables in the round brackets of the method and to separate them with a comma. However, it always applies pay Attention tothat when calling one method always all variables are given a correct value. When calling are the variables again to be separated with a comma.

A method with a string and an int variable would look like this:

staticvoid myMethod (string variable1, int variable2)
{
Code;
}

Exercise example methods with parameters

The current exercise is to write a program that contains a method called show result. Three int variables are transferred to these methods, which must then be added in order to then output the result.

specification

  • read in three int values ​​from the console
  • program a method with the name show result, which contains three int variables as parameters (separated by a comma)
  • in the method show result, add the three int variables and output the result in the method

Click here for the solution. If you have any questions or problems, please use the forum.

Solution:
using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace project1
{
classProgram
      {
staticvoid Main (string [] args)
            {
int number1 = Convert.ToInt32 (Console.ReadLine ());
int number2 = Convert.ToInt32 (Console.ReadLine ());
int number3 = Convert.ToInt32 (Console.ReadLine ());
showResult (number1, number2, number3);
Console.ReadLine ();
            }

staticvoid showResult (int value1, int value2, int value3)
            {
int result = value1 + value2 + value3;
Console.WriteLine (result);
            }
      }
}

Methods with return value

The last type we will look at are return value methods. A method we already know is ReadLine (), this returns a string.Specified will the Return value a method by the word in front of the method name. In the last methods void was used here.
Depending on the type of variablewhich should be returned void is to be replaced with string, int or another variable type. Furthermore, it is necessary to be targeted indicate which value / variable should be returned, this happens with the keyword return the value / variable and a semicolon.

A program excerpt could look like this:

staticstring myMethod ()
{
return "text";
}

In this example the string "Text" would be returned every time it is called. This is used in the same way as with ReadLine ().
you have to be carefulthat there are methods that are not void a return must always be present and this always achievable have to be. If there is a return, for example: in an if query, make sure that there is another return in the else. You also have to pay attention to the right type.
If the return type is int, it is not allowed return "text"; because it is a string.

Exercise example method with return value and parameters

This time a program has to be written which runs the following information:

specification

  • read in two int values ​​and an arithmetic symbol (+/-)
  • programming the method computer, which receives two int values ​​and one arithmetic symbol
  • Depending on the calculation symbol, perform the necessary calculation in the method, if the calculation symbol is not valid, return 0 (return value of type int)
  • in Main, output the result of the calculation

Click here for the solution. If you have any questions or problems, please use the forum.

Solution 1:
using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace project1
{
classProgram
      {
staticvoid Main (string [] args)
            {
int number1 = Convert.ToInt32 (Console.ReadLine ());
int number2 = Convert.ToInt32 (Console.ReadLine ());
string arithmetic character = Console.ReadLine ();

int result = calculator (number1, number2, arithmetic sign);
Console.WriteLine (result);

Console.ReadLine ();
            }

staticint calculator (int value1, int value2, string arithmetic character)
            {
int result = 0;

switch (arithmetic symbol)
                  {
case "+":
result = value1 + value2;
break;
case "-":
result = value1 - value2;
break;
default:
break;
                  }

return result;
            }
      }
}

Solution 2:
using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace project1
{
classProgram
      {
staticvoid Main (string [] args)
            {
int number1 = Convert.ToInt32 (Console.ReadLine ());
int number2 = Convert.ToInt32 (Console.ReadLine ());
string arithmetic character = Console.ReadLine ();

Console.WriteLine (calculator (number1, number2, arithmetic sign));

Console.ReadLine ();
            }

staticint calculator (int value1, int value2, string arithmetic symbol)
            {
if (arithmetic sign == "+")
                  {
return value1 + value2;
                  }
elseif (arithmetic sign == "-")
                  {
return value1 - value2;
                  }
else
                  {
return 0;
                  }
            }
      }
}

continue to section Classes