“ I understand the difference between functions and commands, but I’m still kind of unclear on parameters. Not sure if i can or should use them to send or receive data, when the proper time to use them might be (as opposed to just using a “Set” value within a simple command define to retrieve data) and so on.. “
And I think that’s an excellent question. I can see why you may not understand why we should ever use parameters and by the end of this video you are going to hopefully not only understand why we need them but also start to use them all the time.
Now before we begin I have a little announcement, I just launched Elite Botters, so the website is now live. And so is our first course, it’s called Ubot Studio: Mastering The Fundamentals.
This course is designed to get you from wherever you are right now to a solid understanding of the fundamentals.
And we have some super cool features like fun quizzes, video search and more. So be sure to check it out, there is a link in the description below.
Now what exactly is a parameter and why should you care?
In this context we are talking about parameters which can be defined when you create a custom command or function. To do that you can just drag out the define command and then you will see a section for parameters.
You will notice that parameters have a purple background, this means that its a variable input field, so whatever name you put here will turn into a new variable.
Now it should be noted that this variable is a local variable. And that means that only things inside of the command or function you are making can see it. So that means that you won’t see its value in the debugger.
For this reason, I like to preface it with an underscore, this tells me that it’s a local variable and when I am looking in the debugger I won’t expect to see values for local variables.
So now that you have a rough idea of what a parameter is – let’s talk about why you would want to use one over just a normal set.
And the reason is because it makes the custom command or function independent. So that is to say that it doesn’t need to rely on some outside source of information. Instead, it can take in information and then use it in some way.
Now that probably won’t make too much sense if you are new to Ubotting or programming in general. But don’t worry, this is one of those things that’s better to show rather than tell, so with than in mind lets go over some examples.
Let’s create a custom function which adds some bank balances and returns the result:
Now in this example we are not using parameters, however, as you can see when we run the program, it works!
So, this isn’t making a great case as to why we should use parameters, but let’s see what happens when we want to introduce two new account types.
In this example let’s just say the person has money invested in crypto currencies as well as a stock portfolio and we want to get a combined total of those extra accounts as well.
Well right now our function only adds together the balances of the checking and savings accounts, because it is dependent on those variables. In order to add more accounts we would have to go in and either make another function, change this one or update those variables.
Instead of doing all that extra work, we can actually use the same function. But now we can use parameters to make it more independent and not have to rely on specific variables being set before.
So let’s change our function to now accept parameters so you can see the difference.
Now that we changed it we have to also update the function in our combined balances variable.
So it still works with combine balances, now let’s try it with the extra accounts as well.
As you can see, now that we are using parameters we can use this function whenever we need to add two things together!
With that in mind we can actually take this one step further and also add up the combined balances and combined extra balances to get a total of all the accounts together, all by using the same function!
Now I already know what some of you are thinking, “Why can’t I just always use the same two variables and set them before using the function?”
And the reason is because this will not work in a multi-threaded environment. You can’t set global variables from several threads at once and then send them through a function like this because your data is going to get corrupted. So while this may technically work in a single threaded environment, it’s just bad practice.
So as you can see using parameters can be really powerful. We took a function which was not using parameters and relied on variables being set before, this was only able to add two specific variables together. And then we added parameter so that now it can add any two values. Doing this allowed us to use the function three times in the same program!
If you liked the video be sure to tell me in the comments below.
And be sure to check out our new course Ubot Studio: Mastering The Fundamentals, the link is in the description below.
That’s it for this video and I’ll see you in the next one!