Functions are a super useful way to concentrate code into one area and reuse a certain "feature" across an entire application. For example, if we know we have to calculate the average of two numbers multiple times across a program, it might be worth making a function called get_average(a, b) that returns the average of a and b.
Now, if we take this into the context of this project, we may notice that the line-connecting code for both the 2D graph and 3D graph are very, very similar. In fact, there is really just a one line difference: linez.append(z[star_i]). When we see things like this, then it is almost always possible (and, often preferred) to use a function. So, instead of having two really big blocks of code in the middle of plotting, we could just call a function and have that functionality abstracted away. For example, when plotting the two graphs, we might be able to remove the big blocks of code and just have a line that looks like this:
connect_lines_in_graph(param1, param2, ...)
This will increase the readability of the code because it states clearly what that function will be doing -- namely, connecting the lines in the graph. Further, it centralizes the line-connecting functionality into one place, and we can easily expand or remove functionality as needed.
Then, we can call the function we just defined like so:
name_of_function()
When called, this function will simply output "Begin coding here". So, for this project, we can cut down on code if we use a function to define the line-connecting functionality since the 2D and 3D uses are so similar.
But, this is certainly not necessary and goes beyond the scope of this project. I mostly just wanted to give you this knowledge for the future. Hopefully it will help for down the line!
Functions are a super useful way to concentrate code into one area and reuse a certain "feature" across an entire application. For example, if we know we have to calculate the average of two numbers multiple times across a program, it might be worth making a function called
get_average(a, b)
that returns the average ofa
andb
.Now, if we take this into the context of this project, we may notice that the line-connecting code for both the 2D graph and 3D graph are very, very similar. In fact, there is really just a one line difference:
linez.append(z[star_i])
. When we see things like this, then it is almost always possible (and, often preferred) to use a function. So, instead of having two really big blocks of code in the middle of plotting, we could just call a function and have that functionality abstracted away. For example, when plotting the two graphs, we might be able to remove the big blocks of code and just have a line that looks like this:connect_lines_in_graph(param1, param2, ...)
This will increase the readability of the code because it states clearly what that function will be doing -- namely, connecting the lines in the graph. Further, it centralizes the line-connecting functionality into one place, and we can easily expand or remove functionality as needed.
Functions can be defined by the following code:
Then, we can call the function we just defined like so:
name_of_function()
When called, this function will simply output "Begin coding here". So, for this project, we can cut down on code if we use a function to define the line-connecting functionality since the 2D and 3D uses are so similar.
But, this is certainly not necessary and goes beyond the scope of this project. I mostly just wanted to give you this knowledge for the future. Hopefully it will help for down the line!