8. How to program in C# - METHODS - Beginner Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thanks for tuning in at brakus hello everyone and welcome to video number eight in how to program in c sharp in today's video we're going to have a look at methods this is mostly going to be just an introduction to what can we do with methods and how to rewrite them out but of course we might have an more in-depth look later so what are methods well for the beginner methods are basically a way to group together code to make it much easier to read but once you start getting a bit more advanced using parameters and return types methods are really are really really useful for doing all kinds of of things such as reusing code organizing code and and really making your code more flexible so methods are also commonly known as functions in c-sharp and that's because it's an object-oriented programming language so those two terms are interchangeable in other languages they might not mean exactly the same join it generally you can say that a method will always be a function a function won't always be a method though but in c-sharp it will so before we get started today I just quickly want to mention that i if you are a rapid unity user i've created an extension for unity called UPA toolkit where you can make pixel-art inside of unity itself if you don't know you know decent awesome game engine which I have plenty of tutorials on and I've just relieved it released a new version way which introduces a layer system so you could get that on the asset store just wanted to do a quick plug that cool so let's open up our salmon studio project I'm going to zoom in on the code here by the way I've been getting quite a few questions as to why I'm using salmon studio trust me it's not really something that I enjoy very much I don't think that examine studio is the best IDE out there by any means but it's very accessible it's very easy to use and it's very Abell for both pc and mac which was a requirement for this tutorial if you are using a PC or Windows computer I definitely recommend that you check out Visual Studio instead that's Microsoft's own IDE and if you're using Mac you have something called Xcode available which is also really really good but if you want everything to look exactly like I have it here trust me for the things we're going to be doing seminarian studio is fine cool so let's take right into it as you can see here whenever we start up with a project we have a function here or method called main and we've just ignored this until now except for this comment here that says it's a method called main and it's called when the program starts because pretty much any kind of application has what is called an entry function and this is basic basically a method that is called when every the program starts and this is where you start calling other functions and your whole program begins because it would be a really borrowing program if nothing happened when it started so that's basically what this is when you're doing c-sharp the main function is the start function and then from here we can do some code and in here we've written all kinds of code we have done while loops and for loops but what if we have this larger application let's say we want to have an application that does multiple things well instead of writing everything out and just continuing on and on and on in the main function we can split this up into different methods so for example if we wanted to agreed the user we could have a method that does this so let's create one now so under the the public static void main we're going to type another method down here make sure that it's inside of the same class and of course inside of the same namespace so just under here we're going to type public just use the same syntax as you see up here static void and this for now is what we are going to type in front of every function then you're going to type in the name and we're going to maybe type in greed user and then what you want to do and you want to have this for any method of any kind you want to have an open and close parenthesis and you notice that in here we have what is called an argument we are not going to have that yet we're going to look at that later and then we're going to open and close some brackets I'm just going to bump this down here to make it more readable and then in here we can write some code for example we could say console dot write line and we're going to type in hello world like this and then what we do up here is we can call this method so this is basically how we define a method and this is a method definition and this up here is going to be a method call so we're going to type in greed user we're going to put in the parentheses and we're going to end with a semicolon you might see that this looks very familiar to console that read key but we do exactly the same the reason why is that concert that rekey as with pretty much anything we've been doing until now is a method the same is console.writeline that has an argument and and so on so the programming is really just calling and defining a bunch of methods in order to do different stuff so now when we run this program it's going to start out in our entry function which is the main function it's going to call the greed user function so it's going to go down here and it's going to write out hello world then when it's done with that it's going to go back here and it's going to print out console or it's going to wait for an input so let's try it and hit play you can see it opens up a console window hello world is printed and when we type something it closes so that's a the most basic use form for a a a method much cool about this is that we can have different methods we can have them call each other let's say we could maybe call another method here that would maybe do and let's say if we wanted to after greeting the user we could maybe say wait for user input then we can have that as a function or we could have it up here wait for some user input then we could maybe process input and then we could maybe print output so and this is what many simple programs look like it's just on these different groupings of functionality into functions that we then call in a specific order so the cool thing about this is that it's not only used used to group together functionality it's also used pretty much as a utility of some sort I like to think of methods as a machine where are you tell the Machine that it needs to do something and then it does so but the machine can get more advanced you can also give the Machine some kind of input and then it does something with that input and if the machine is really awesome it will also give you an output back so let's have a look at the first instance here where we just say that the machine should do something and it does that's what we have here the second instance is where we give some the Machine some input that it then does something with let's try writing one of those methods so I'm going to delete this here and delete this here so again we start with public static void we call this maybe add let's say we want to add to two numbers together and then print out the result so we open and close the parenthesis and we put we open and close some brackets here and in here we can then add together some numbers but let's say up here in the main function we know which numbers we should add when well then we need to some way of calling the method but also giving it some numbers that it should add together this is where arguments come in so arguments or parameters if you will a way of path passing input into a method from the place that it's called so if we say that we want to add together two integers we can type in here in CH int num1 this looks very much like available take declaration and really it is comma two another one int num2 now we know that this method wants to have passed in two numbers say we were separated by a comma so up here we can now write maybe four and eight and you can see that it wants here an input of first number one and then number two and both of type integer so this is the way that we pass input to the function and then inside of the method itself we can then do num1 plus num2 and we can maybe just print this out so we can do console dot bright line num1 plus num2 let's try this out so hit play you can see it writes out the number 12 and we can of course give it any two numbers that we would like up here ten and eight that's going to write out 18 and so on what you can also do is have multiple different methods with the same name but with different different arguments so let's say that we have another method called add that doesn't take in just two arguments but takes in a third one to numb three here and we're going to add numb three down here two and then up here we can first add together ten and eight and then we can maybe also add together ten eight and um - and we can see that it just automatically knows that this should call the method here and this should call the method down here let's try this out again the play and it it shows the numbers 18 and 20 hmm great great so that was the second instance if we want to give our method some kind of input I'm just going to delete the second method there and the method here but I'm going to keep the add method because let's say that we don't want to just print out the numbers but we want to use the result of the calculation up here so we can use it for all different kind of stuff maybe checking if it's larger than some value maybe printing it out maybe using it for a further calculation just as part of some calculation then we can do all kinds of stuff up here if we have some way of retrieving the result from the method so in order to do this instead of writing out the result here we use what is called a return type and the way you do this is instead of writing public static void we write something else right here because what void actually means is return nothing that's what we type when we don't want the method to go back and insert anything here when we just wanted to do something here with the input that it's given but if we wanted to give something back we can type that we right here the type that we want to give back in this case we want to give back an integer number so we write int here then what we can do is instead of writing console.writeline and then adding the numbers together we simply write return followed by what we want to return so we write num1 plus num2 so now what this method does is it when it is called it's going to take in two numbers it's gonna them together and it's going to give back the result that's going to return an integer value that we can get then use up here so what we do up here in order to use this value and right now we are simply going to write out the result so we're going to say console dot write line and then in here we can use this basically as any other variable we can simply go like this or maybe we could store it in a variable here called result so we're going to type int result equals add like this so now we can simply say that different variables should equal the result of this calculation because we are returning a number and then in here we could write out the result so this makes our code much more flexible it makes the method here much more reusable and it just looks very cool so now when we hit play you can see that as before it writes out the number 18 but now we're able to do much more with this for example we could check if result is greater than 10 then we want to console dot readline result is larger than 10 and if it's not else console dot readline result is smaller than or equal to whoops 210 so and by the way this is just shorthand notation I'm just leaving out the brackets here I'm just going to put them in to make it a bit easier for you to follow I'm just way to use to not not typing them when you only need one single line of code you can just leave them out but it is generally better practice to to get them in there because sometimes you just want to add something to them there and then then you're going to put them in anyway so like this so now you can see that when we are getting the value back like this we can do different stuff with it such as checking the result which is what we're doing here so if we if we play now we should see that the result is larger than 10 and indeed we do and if we maybe type in our 1 and 8 it's going to say the result is smaller than or equal to 10 so that was this introduction to methods with this you're going to be able to write the majority of methods yet you're going to write in your programming career if you understand return types already that's really great a lot of people have a hard time understanding exactly what that does if you don't well then you were probably part of the majority and for now you can just type after me but it's good to just know that it exists and that don't worry you will master it I sure was very confused about this when I started so great thank you for watching the whole way through this video I hope you've learned something and I'll see you in the next video
Info
Channel: Brackeys
Views: 300,248
Rating: undefined out of 5
Keywords: unity, unity3d, tutorial, asset, assets, model, texture, models, textures, material, materials, beginner, easy, javascript, java, function, and, settings, how, to, howto, learn, learning, course, series, tips, tricks, tutorials, workflow, fix, tip, technology, game, development, develop, games, programming, coding, basic, basics, C#, functions, method, methods, type, types, field, fields, group, return, value, argument, arguments, parameters, define, call, flow, chart, introduction
Id: QwygwfqOHsI
Channel Id: undefined
Length: 17min 7sec (1027 seconds)
Published: Sun Feb 22 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.