Related Articles

Back to Latest Articles

C# Basics – Functions – Part 10

We get to know how functions work in C# programming language.

Andraz Krzisnik
C# Basics – Functions – Part 10


We’ve been writing simple programs so far. But when things get more complicated, writing it all into one block of code could become hard to read. We can use functions for executing a block of code whenever the name of the function is called.

Functions are very useful at making your code more readable and easier to fix any errors. We can use a function more than just once and we can even use it in other programs. We have always been using functions through entire tutorial, for example Math.Sqrt() is a function that is already written.

There are a few different ways we can denote functions. We separate static and object functions. We also separate them by what kind of access we have with this function, these can be public, private and protected. For now we will only work with public.

For each function, before we use it, we first need to define it.  First thing, we have to declare, is what kind of access we have to this method (public, private, protected), then we need to set what type is our function, static or objective. What follows after this, is the data type of value our function should return. After that, all we need is the name of the function, and possible arguments this function will take. And lastly, we have to write our code between curly brackets ({}).

public void myFunction (int argument1, string argument2)
Download Examples Next Chapter
Show Comments (0)


Related Articles

C# Basics

C# Basics – Random Numbers, Chars and Strings – Part 7

Random Numbers To generate a random number, we will use a constructor called Random. Then to generate a random number we will use either .Next() or .NextDouble() function. Random...

Posted on by Andraz Krzisnik
C# Basics

C# Basics – Conditional Statements – Part 5

Conditional Statements There is a data type for logic operations, called bool. Possible values that this kind of variable can hold are true and false. Let’s take a look at logic...

Posted on by Andraz Krzisnik