In the previous lesson, we learned how to encapsulate our code, but we do not know when we should add in our cake from previous lesson those components! That’s why today we will learn the function or the so-called methods, you know how look “Main” is a function.

Methods

Method is the same as the function only the function inside the class is called method.

Each method has the following scheme:

Let’s write it in the form of a code:

The above method has public access, does not return anything, is called Example and accepts only one argument, if we will call this method will execute the code in the clamps, the name of the method does not matter We can call it ourselves as we want, the arguments we can also call as we want.

According to the rules of code purity, the function can take up to three arguments, ideally if there are no arguments at all, because it is also possible, of course the function can take a lot more arguments than three, but in terms of code readability it will not be good.

Let’s create in our Cake class this method:

And calling this method in the Main function in this way:

After entering the quantity of ingredients, and description of the product, we will see the text that we typed in the body of the function, this is how the function is used, I will show how to use a function that returns a type, example below:

Let’s put something like that in the Cake class and in the Main function:

I think that there is nothing difficult here.

First, in the Main function, we introduce the baking time manually and call the BakingCake method which accepts our entered value, in the body of the function we change the value of the private field _bakingtime to the value we introduced, ie the variable time.

And by the way you have learned that private fields can also be changed using methods, java programmers do this because in java there are no accessors get and set

Then we create the variable text in return it in this function, and we go back to the function Main and we attribute the returned value to the variable time and display this variable on the screen, it is not too difficult.

Static and instant methods

So that we could call methods in the Cake class, we first had to create a cake object and then enter the name of the object and after the dot the name of the function, right?

However, you can do it in a different, easier way as long as the method is static. Static methods are created like this:

Also, the _HowMuchFlour variable must be changed to static:

And calling her looks like this:

The difference is that you do not have to create a class object.

And that’s all, it is simple, right?

The whole Cake class code and Main function looks like this:

This content also you can find on my steemit blog: https://steemit.com/programming/@slawas/c-language-8-methods

And on my blog: http://devman.pl/csharplan/c-language-methods/

That’s it for today, in the next lesson we will learn about constructors and destructors, see you!

Entrepreneur and software developer