From the course: Learning C#

Function basics - C# Tutorial

From the course: Learning C#

Function basics

- In this chapter, we're going to learn about how to define and work with functions and sometimes they're also called methods. And we'll learn more about that when we get to the object oriented programming chapter. Functions are used to group related blocks of code together so that they can be easily reused and parameterized so that their logic can be used with different values. So let's take a look at some simple examples. So here in the Start folder in Functions we'll open up the Basics folder and open the Program and I'm going to be using C# top level statements here, I'm not going to be using the whole namespace in class program. We learned about this earlier in the course, so I'm just going to use this to just demonstrate some functions. So first we're going to write a function that converts miles to kilometers. Functions can have a return value which in this case is going to be a floating point number. So I'm going to start off by writing the word float, and then that's going to be followed by the name of the function, and I'm going to call it MilesToKm. And then there's an optional list of parameters inside the parentheses and each parameter has its own type. So it'll take a floating point number named miles as a parameter. And then we have the two curly braces and the code goes in between those two curly braces. So what we're going to do is we're going to define a variable named result and that will be a floating point number, and it's going to be equal to miles times 1.6f. I have to have that f there for the floating point number. And then I'm going to return the value when the function ends. So now, we have an encapsulated block of code that we can call with different values. So for example, let me go ahead and Console.WriteLine and I'll define an interpolated string and I'll type "The result is" and then we're going to call MilesToKm with 8.0. And then I'll do the same thing with 52.0. And functions can also be defined with no return value. In which case they have a return value defined as a void. So if I say void and I'll call this function PrintWithPrefix. And in this case, the function will take a string argument. And by the way, functions don't have to take arguments, you can just simply leave the parentheses empty and that's perfectly valid as well. But in this case we'll have a string parameter called theStr and all PrintWithPrefix is going to do is call Console.WriteLine, and it's going to right out that little prefix along with whatever string was passed in. And now we can add some calls to the second function as well. So we'll call PrintWithPrefix and we'll call that with Test String. And then we'll also call that with Another Test String. So let's try running these. So we'll right click and open the terminal, and we'll run this dotnet run. So the program will compile and then you can see the results. So my first function I'm converting eight and 52 miles to kilometers, as you can see the results and you can also see my PrintWithPrefix, it's printing each string with this little prefix in front of it. So that's a basic example of how to define functions and functions basically, they have a return type, either a type or void along with the name optional parameters and then the code goes in the curly braces. So we're going to use this as a starting point for the rest of the chapter and see how we can use functions and to find different kinds of functions to take different kinds of parameters. And we'll finish up with another programming challenge.

Contents