Tutorial 208 | Why use Methods?

  • 2 days ago
A Markplex subscriber asked what were the advantages in using methods in EasyLanguage. What is the point of them and are they really necessary? This tutorial will attempt to answer these question and gives various examples of simple methods.
https://markplex.com/free-tutorials/tutorial-208-why-use-methods/
Transcript
00:00So if we wanted to calculate an average what we'd probably do is just use the
00:04built-in function which is average and we would put in our variables close and
00:11say 14 and then we would plot that value so let's just say plot 1 and we're going
00:20to plot value 2. Plotting the average. I'm just going to verify that and then add
00:27that to a chart and I'm going to move it on to the main part of the chart. Okay so
00:40that's one way of doing it. What we could also do is we could say well let's have
00:45a look what's inside the function average and we could say well let's just
00:51calculate it directly so we're going to say that it's calculated using this
00:55formula here. I'm just going to copy that, close the function and we could say okay
01:02let's do the calculations so we could copy that and say value 1 is equal to
01:12summation. I'm just going to instead of using an input we're just going to put
01:15in C length 14 and length 14. We need to put in a semicolon that's going to be
01:29value 1 and let's just plot value 1.
01:38So you can't actually see anything but if you go over the bars you can see that
01:50the value of the plots are identical and what we could do is just go in here and
01:57with the study we're looking at we could just for example make value the first
02:03plot that thick, the second plot maybe that thick and we could just change the
02:11colors so maybe plot 2 is pale blue and then you can just about see that the
02:18plots are the same. Now what if we wanted to do this as a simple method and in the
02:25tutorial I've explained various reasons why we might want to use a method. This
02:30is probably realistically not one of them but let me just show you how we go
02:36about doing that. So let's create the most simple method that we can. We'll
02:42call it method. I'm not going to return anything. I'm going to say void and calc
02:55average. I'm going to give it that name. We don't have any parameters so I'm just going to put
03:02begin, end and then the calculation is going to be essentially the same
03:10calculation as here but what we're probably going to do if it's a method is
03:18want to have some inputs that we can use and we're going to have the inputs
03:22coming from the main parts of the program. So let's create some inputs and
03:29variable and we're going to use that in the method so we're going to say that this variable
03:36average whose scope is the whole program is equal to summation, let's copy this and then we can
03:44change the C to price and the length input. Verify that and let's just plot 3, the result of that method and you'll notice it's plotting, it's calculating
04:11average and that is the scope of that is the whole program. So we're not returning anything but what we're doing is putting the result of the calculation into a variable average. So verify that and go back to the program. So we notice there's a problem here it's saying that the value of that newly calculated value is zero. Well of course the reason for that is
04:41that we have not actually called this method and what we actually need to do is call the method on every single bar and then we'll get the calculation done and we call it by just typing in calc average and verify. We go back to the chart and you can see just by going over
05:10those values that they are identical and again what we could do is go to our program and change plot 3, could give it a different color, I don't know, orange perhaps and we could set the style to be the thicker style available and then you'll see that that's plotted on the chart.
05:38Okay so that's the most simple method that we could use. Let's make it a little bit more complicated. So I'm going to copy the existing method because we're going to be using some of the same structure and I'm going to call it calc average 1 to give it a new name and what we're going to do this time is we're going to declare a variable just inside
06:08this method. So let's call that average 1. We need to tell it what it is, it's actually a double and we're not putting any value in brackets so we don't have brackets and then this is going to be average 1 and we've got the summation there. This is calculating something in the method but what we don't have is access to that very
06:38variable outside the method so let's just let's call the method again on every bar. No parameters again and let's try let's just see what happens when we try and plot the value.
07:08Okay study not verified. Well the reason for that is that the program as a whole doesn't know anything about average 1 because the scope of that is just within this method so we need to do something about and what we can do is make instead of a void we're going to return a double and having done the calculation we're going to say return average 1.
07:39So now when we call this we have to say value 3 or a value variable name equals calc average 1 because we're now returning a double and instead of saying plot average 1 we're actually going to plot value 3.
08:03So verify then go to the chart you'll see now that you can't see the plots but if you look at the value per bar you'll see the plots 1, 2, 3 and 4 are identical as you would expect.
08:21Now we're going to create one more method here so I'm just going to copy this again and this time we're actually going to add a parameter so call it calc 2 and we're going to put the parameter in here it's going to be a an integer and let's just call it len for the sake of argument.
08:50Then we're going to be using that in here now what you'll notice we've done instead of now using this input we've got an input to the method itself and calc average 2 so let's just see how that works.
09:11So value 4 equals calc average 2 and now we have a parameter so I'm just going to put in 14 and again let's plot the value.
09:35So plot 5 is value 4 and if we go to the chart and go over the bars you'll see again that all those values plot 1, 2, 3, 4 and 5 are identical.
09:54Now part of the reason for doing this tutorial was that somebody's asking why do we need to use methods anyway well a good reason for that is it allows you to take some complex idea or specification and break it down into smaller parts and then when those smaller parts are created they're available for reuse of course in different programs but they're also smaller parts and easier to debug.
10:24But another reason is that if we were to go to the toolbox and just pick any of these for example price series provider click on that so our program now includes a price series provider.
10:42If we go to the properties of the price series provider you'll see this little event icon if we click on that then if we want to have an event so for example when the price series provider is updated what we could do is just double click here and you'll notice as soon as we do that that we get this method appearing in our program.
11:07So essentially what happens is if the price series provider that we've just set up would obviously have to change the properties to explain what it is but if that price series provider is updated then we have this this method running and you would of course put some things in here print statements logic etc so that is another reason why we need to use.
11:36I would probably probably need to use methods anyway hopefully that might be useful I'll put a fully commented version of this available for GoldPass members at no additional charge and hopefully this tutorial will be useful please send me any questions.
11:58If you're not on the Markplex mailing list then please go to markplex.com and join there and also please consider joining GoldPass.
12:08Thank you very much.

Recommended