 Hey everybody, this is Brian. Welcome to the 13th C-sharp tutorial. Today we're going to be discussing functions. Well, right now you're looking at a function. Static void main. That's your main function. A function is just a block of code that gets executed. Notice these brackets here. Anything within those brackets is going to get executed when we call the main function. Now There are different types of functions and we're going to cover them. Notice the static keyword? Static means there can be only one instance of that function. That's going to sound confusing and we're going to cover this. Void means it returns nothing. Main is the name. And then in parameters, in brackets you have the parameters string, which is just an array of string called arguments or args. So that is our function. So go ahead and create a new class. And let's just call it cat because yes, I like cats. And in here, let's just say Let's make a public string name and then let's say public void yes Let's capitalize the h there and we're just going to say console right line So that is a function right there. We've just made one. There's the code block. Whoops. There's the code block that gets executed Public access modifier example just like with our variables. We can make it public or private Public means it's available the outside world private means it's internal to this class only this class can access it Void once again means it returns nothing We have the name and then the arguments list and there's none. So we just have the brackets and then the code that we're going to execute So let's actually just create An instance of that cat class. Whoops. There we go Little keyboard challenge today. It's been a long day. And then there's our his function Now you notice how you have to have the brackets behind it that's because in C sharp you have to have the parameter list even if it's empty and Let's just run this and whoops. We forgot our Handy console read keep the window open here Notice the brackets behind read. That's right read is a function If you hover the mouse over he says int console read What that means is that's returning an integer instead of void it has int So there's his So what we're doing here, and let's kind of break this down a little bit You have a class called cat, which is our blueprint. You're creating a variable and creating a new instance of that class in other words, we're creating an object That object we're calling the his function inside of that object So when it says ccat.his it's taking that instance out of your blueprint Going to his and executing the code inside of it No voodoo magic. It's actually pretty simple So what about static you see how it's a static void main blah blah blah? Well, let's create a static function here and once again, I'm a big fan of copying and pasting. So let's just Do that Notice how the only change in the syntax is we've got this static keyword in here So what does static really do for us? Well, if you go back into ccat Notice how meow is not in there even though we gave it a public access modifier. Well, why is that? The answer is pretty simple Static simply means there will be only one and We have to back up a little bit if you make a hundred instances of this cat class Each instance is going to have a his function. So in turn you're gonna have a hundred his functions in memory Static means there'll be only one meow function Because there's only one you don't even need an instance of this cat class because it's created automatically To prove that you can just say cat Meow notice how meow is in there, but his isn't it's because meow static Very simple very easy So let's run this and you'll see both of them fire off hiss and meow So there we have hiss coming from our object and meow coming from the static function, which is not even an object We're just calling it directly out of the class So that in a nutshell is how you do a function now. Let's do a function that returns something so let's say a public And we'll say age Equals zero because you always want to give it a default default value Dot net will default it to zero just for you, but it's always safe to do it and then we're gonna say public and Get age we're gonna say return H So all we're doing here is we're saying okay. We have a function. It's got a public access modifier It's return type is an ant notice how we've been doing void meaning it returns nothing now We're gonna return an end and you can return any type this could be a string It could be an int it could be the actual cat class itself or another class and then get age is the name and then we have some arguments and We're just returning the age So let's just do this and let's say console not right line See cat Get age and let's actually set the age of this cat. Let's say cat equals And Ophelia my other kitty is 18 years old my old cat Whoops for you at the age part She gets memory goes when she gets old So let's run this And there's our 18 So what we're saying is console right line C cat dot get age notice the Brackets or yeah, the parentheses and it's in parentheses to so what we're really doing here is we're calling two functions We're saying right line because right lines a function notice It says void console right line meaning right line returns nothing its parameter list is an int value And there's 18 overloads That's something we're gonna get into in the next topic But for right now just understand that when you call console right line, you're calling a class the static method Right line and then your arguments are C cat dot get age. So you're calling another function Now let's make one Let's call this set name. Let's actually call it say age. Sorry, and we're just gonna say age equals I age and let's not call it I it's let's call it an age. Sorry Now notice how we forgot the void so it's barking at us a method must have a return type Well, we don't want to turn anything so it's gonna be a void This is what in Java you would call a getter and a setter now Dotnet handles it differently. They have a get and a set method which we'll discuss soon very soon in another tutorial But let's just stick with this because it's very simple for you to understand You have a value or I should say variable of age It's initial value zero. You're saying get age returns the age Set age and there's an age if we're saying age equals an age So we have a get and a set So let's just do You guessed it Set age and let's say 18 So we're calling the objects set age function. We're giving it the 18 and We're going to say console dot right line secat dot get age and it still says 18 Now what if you wanted multiple parameters here? Well, let's just kind of copy this Hmm and let's do something a little funky. Let's just call it funky and we're gonna say int times and String action And then we're just gonna say console whoops right line and we're just gonna say Action and don't get confused by this plus sign We're just making a string and we're concatenating it. So we're just saying action plus space plus time We're not actually doing any math here. We're just making a string and we're saying add this to this to that So we're just making a big string and I used funky just because well, I ran out ideas. So we'll say oops secat let's call funky and Notice how there's int time and string action. So those are two parameters Now, let me show you a little bit about c-sharps self-documenting methods Let's say we want to hand this class off to another person. What is it time and string action really do? What you can do is just you know the double slashes for a comment Do three slashes. You notice how he puts this code in here. It says summary. Well, that's XML documentation pram name time pram name action. Well, what is this? This is how you document your code. It's very simple. Give it a description Let's get funky and Give it a description for that specific parameter and let's say Action to perform and let's actually just fill this out while we're here and we're gonna say for and i equals zero i is less than time i Plus plus because we're gonna increment i. This is our for loop that we've done And we're just gonna put the i here. So however many times you tell it to do it. It's gonna just do a for loop so funky and You notice how it says let's get funky Right here because that's our description now we got to give it a time and let's just say eight You know something here. It says action the action to perform you filled that in see if we hit that Remove it says time the number of times to perform So it's very simple very easy right on the screen. You can see exactly what this is going to do and we'll just say Jump Want our cat to jump because the cat's getting a little weird had too much catnip Hope if I spelled jump right, maybe I've had too much catnip. All right And there you see jump zero one two three four five six seven eight Actually just seven Because we said eight so it starts zero based remember so let's review really quick. I know we've kind of covered a lot of ground here We're talking about functions Functions have an access modifier in this case public Avoid hiss void means nothing this is the name no parameters. There's our code block right here Then we talked about static static means there's only one so you don't need an instance of your object You call it just by calling the class name cat meow And then we talked about how you can return a value from your function Simply by giving it a type instead of a void and then we talked about how you can have parameters And we've also talked about self-documenting code all this gobbly. Gook and Then how to have multiple parameters and how to do a little more advanced stuffed inside of our function There's our for loop which we've covered in previous tutorials and Yes, the static void main that you guessed it is just a function Pretty simple pretty easy, but you can see it's actually very powerful. You can do a lot of things with these So this is Brian. Let's all for this tutorial. I hope you found this educational and entertaining and Give me a shout. Let me know if you guys got any feedback. We're gonna start Stepping through these C-sharp tutorials rather quickly because I believe it's a very powerful yet easy language to learn