Posted 31 January 2016 - 08:49 AM
Hello! I've got some tips and such for proper function usage.
First, let's start off with something I like to call code compression. Let's consider this situation. You have a computer, and you wish to connect it to an external monitor. Typically, you have to declare variables, set cursor positions, and write text. Afterwards, you might have a large program, but what if there was a way you could compress, say, ten lines of code, into five.
Here's a typical program
First off, there is nothing wrong with writing like this. There are more efficient ways of writing code though, and as I'm sure you noticed, the calling of 'mon.setCursorPos(x,y)' and 'mon.write(text)' were called often. In ten lines of code, we managed to output five lines to the monitor. I will now show you a way of compressing this code into a function.
Let's walk through this. The first thing we did was declare the variable, mon, to store the peripheral, monitor_1. We then go on to create a function, which will accept three arguments. Arguments are, in a way, variables for the function. We can use these arguments for any purpose we want. They can store decimal values, integer values, string values (string is a fancy way of saying text), or even Boolean values.
In this case though, we use the x and y arguments to store integer values, or whole numbers. The x argument will be used to set the cursor on the x plane, or more simply put, we set the cursor towards the left or towards the right of the monitor. The y argument will be used to set the cursor on the y plane, or more simply put, we set the cursor towards the top or towards the bottom of the monitor. The final argument will be used to write a string to the monitor.
Let's get to the interesting part of this new program. We start off by clearing the monitor but using the mon.clear() command. However though, recall that previously we used one line of code to set the cursor position of monitor and another line of code to write the text to the monitor at that specific location. By using a function, we were able to compress two lines of code into one by calling on that function and providing it with arguments.
We then call on the function by writing our function name and providing it with all the arguments we listed. Our function is called Pos in this tutorial, but you can name your functions whatever you wish. You can also provide as many or as few arguments as you wish, but in this case, our function takes three arguments. The first time we call this function, we provide the x argument with an integer value of one, and we do the same with the y argument. For the string argument, we provide "Hello there!". When the computer calls on this function, it will provide the function with the values you provided.
If we look back to our function, we see that mon.setCursorPos(x,y) will be given the values of 1 and 1, and mon.write(string) will be given the value of "Hello there!"
To recap, functions can be used to compress code or to save time on writing repeated commands, like setting cursor positions and writing text. Functions can be given any name, but remember, they are CASE SENSITIVE! We can provide functions with no or many arguments, but remember that each argument should be used within the function. It'd be silly to declare a z variable in our Pos function if we had no use for z. And finally, we call on these functions by writing the function name, followed by all the arguments needed for the function to operate properly.
Here are a few more examples, cheers!
I do hope this helped you. I'm open to suggestions and criticism, as I always want to improve my coding and practices. If anything's confusing, I'll try to re-explain it so it makes sense. Cheers!
First, let's start off with something I like to call code compression. Let's consider this situation. You have a computer, and you wish to connect it to an external monitor. Typically, you have to declare variables, set cursor positions, and write text. Afterwards, you might have a large program, but what if there was a way you could compress, say, ten lines of code, into five.
Here's a typical program
mon = peripheral.wrap("monitor_1")
mon.clear()
mon.setCursorPos(1,1)
mon.write("Hello there!")
mon.setCursorPos(1,2)
mon.write("=========")
mon.setCursorPos(1,3)
mon.write("This is a test program.")
mon.setCursorPos(1,4)
mon.write("This is a long way of writing to a monitor")
mon.setCursorPos(1,5)
mon.write("What if there was an easier way?")
First off, there is nothing wrong with writing like this. There are more efficient ways of writing code though, and as I'm sure you noticed, the calling of 'mon.setCursorPos(x,y)' and 'mon.write(text)' were called often. In ten lines of code, we managed to output five lines to the monitor. I will now show you a way of compressing this code into a function.
mon = peripheral.wrap("monitor_1")
function Pos(x,y,string)
mon.setCursorPos(x,y)
mon.write(string)
end
mon.clear()
Pos(1,1,"Hello there!")
Pos(1,2,"=========")
Pos(1,3,"This is a test program")
Pos(1,4,"This is a short way of writing to a monitor")
Pos(1,5,"This is an easier way!")
Let's walk through this. The first thing we did was declare the variable, mon, to store the peripheral, monitor_1. We then go on to create a function, which will accept three arguments. Arguments are, in a way, variables for the function. We can use these arguments for any purpose we want. They can store decimal values, integer values, string values (string is a fancy way of saying text), or even Boolean values.
In this case though, we use the x and y arguments to store integer values, or whole numbers. The x argument will be used to set the cursor on the x plane, or more simply put, we set the cursor towards the left or towards the right of the monitor. The y argument will be used to set the cursor on the y plane, or more simply put, we set the cursor towards the top or towards the bottom of the monitor. The final argument will be used to write a string to the monitor.
Let's get to the interesting part of this new program. We start off by clearing the monitor but using the mon.clear() command. However though, recall that previously we used one line of code to set the cursor position of monitor and another line of code to write the text to the monitor at that specific location. By using a function, we were able to compress two lines of code into one by calling on that function and providing it with arguments.
We then call on the function by writing our function name and providing it with all the arguments we listed. Our function is called Pos in this tutorial, but you can name your functions whatever you wish. You can also provide as many or as few arguments as you wish, but in this case, our function takes three arguments. The first time we call this function, we provide the x argument with an integer value of one, and we do the same with the y argument. For the string argument, we provide "Hello there!". When the computer calls on this function, it will provide the function with the values you provided.
If we look back to our function, we see that mon.setCursorPos(x,y) will be given the values of 1 and 1, and mon.write(string) will be given the value of "Hello there!"
To recap, functions can be used to compress code or to save time on writing repeated commands, like setting cursor positions and writing text. Functions can be given any name, but remember, they are CASE SENSITIVE! We can provide functions with no or many arguments, but remember that each argument should be used within the function. It'd be silly to declare a z variable in our Pos function if we had no use for z. And finally, we call on these functions by writing the function name, followed by all the arguments needed for the function to operate properly.
Here are a few more examples, cheers!
mon = peripheral.wrap("monitor_1")
function Pos(x,y,string)
mon.setCursorPos(x,y)
mon.write(string)
end
x = 0
while true do
Pos(1,1,"Value: "..x)
sleep(0.5)
x = x + 1
end
*Uses an infinite while loop to display the increasing value of x
mon = peripheral.wrap("monitor_1")
function Pos(x,y,string)
mon.setCursorPos(x,y)
mon.write(string)
end
x = 0
while true do
if(x == 1) then
Pos(1,1,"X is 1")
elseif(x == 2) then
Pos(1,1,"X is 2")
elseif(x ~= 1 or x ~= 2) then
Pos(1,1,"X is neither 1 nor 2")
end
sleep(1)
x = x + 1
end
*Notice that by calling on our function, our code looks neater, and we kept ourselves from writing an additional line per if statement.I do hope this helped you. I'm open to suggestions and criticism, as I always want to improve my coding and practices. If anything's confusing, I'll try to re-explain it so it makes sense. Cheers!