Day Three of the #60Days of Flutter Challenge
Welcome to Day Three of my sixty-day-long project to learn mobile development with Flutter.
Today we looked at lists, functions, parameters, and arguments. Similar to the first two days, we are continuing to concentrate (mostly) on syntax.
Lists are structures that show multiple pieces of ordered data. In essence, they are a variable that holds more than one thing.
There are four parts that go into a List:
(#1) The word List (#2)<list type> (#3)theName = (#4)[the, items, in, the, list];
Lists are put in brackets, and a comma separates each item.
In the above example, the list — named favNums — is of type integer.
It is made up of five items (all numbers) in four index positions.
Similar to the counting of String variable characters in earlier lessons, we start at index position 0. To calculate the length, we begin at the number 1.
We can do some cool stuff with lists:
To print the length, we add .length to the list name.
To reassign a particular value, we type the list name (in this case, favNums) and put the index position we’d like to change in brackets.
In the above example, we are reassigning index position 3 with the new value of 39.
Adding and removing items in a list is done with .add() and .remove(). In the above example, we added the number 84 to the end of the favNums list and removed the number 4.
Functions are a collection of code that can be reused. It is important to write as little code as possible and reuse it as often as you can.
It’s cleaner and therefore easier to read that way.
In the above example, the function draws a small triangle. Easy enough and not necessarily worthy of a separate function. However, what if you needed five triangles? or 500? What if the triangle needed to be redrawn more than once?
A separate function allows you to code once and reuse it as needed.
DRY — Don’t. Repeat. Yourself.
One last quick note about this — no matter what, the control flow of the main() function still goes from top to bottom.
Parameters and arguments make functions more robust and reusable.
In the above sayHi() function, we are adding the String parameter username. Doing this means that when we call sayHi() in the main() function, we need to pass in an argument.
To do that, we add an argument to the function call — in this case, sayHi(“Joe”);
The same logic holds regardless of the number of parameters you consider. In the below example, we have added int age to the sayHi() function. Therefore, when we call sayHi() in the main() function, we need to account for both the String username and int age.
Whenever we create a parameter, we need to input a corresponding argument for the code to run correctly.
Lastly, after dealing strictly with void functions for the first couple of days, we add functions that return a value to our skillset.
Notice that the function value in the above example of return type double. Doing this means that whatever code you add, the function has to end with a return value.
This is an important tool to use, as information can be stored in variables to do (I’m assuming — Mike hasn’t officially gotten there yet) all kinds of cool stuff.
That’s it for Day Three. While this wasn’t necessarily the longest section of the tutorial video (only about 34 minutes), it felt like it was all information that will soon serve a bigger purpose.
So far, each day is building nicely on the previous day’s learning.
On to day four:
In case you missed it, here is the post for day two: