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 – Arrays – Part 8

Arrays It’s an often occurrence, that we are dealing with a bunch of data of the same data type. And in case we want to process each value in the same or similar way, arrays come...

Posted on by Andraz Krzisnik
C# Tutorial

C# Tutorial: How To Create An Image Negative

A negative image is a complete inversion of an image, we would say to be normal. In other words, dark areas will appear light, and light areas will become dark. More in detail, a...

Posted on by Andraz Krzisnik