Navigation

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

Functions

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 ({}).


Download Examples Next Chapter
Show Comments (0)

Comments

Related Articles

C# Basics

C# Basics – Classes – Part 11

Classes Class is basically a container of functions. It can be a separate file or it can be written in the same file as your main project file. We can also create a custom object...

Posted on by Andraz Krzisnik
C# Tutorial

C# Tutorial: How To Flip an Image Horizontally

Where Do We Start? There are numerous ways you can flip an image horizontally. It’s pretty basic functionality in image processing. I wanted to challenge myself and not just...

Posted on by Andraz Krzisnik