|
>>
|
No. 1534238
File
140950119818.png
- (50.95KB
, 200x200
, 140726800873.png
)
>>1534197 Okay, so that line is composed of two words and what is known as an "operator."
The first, "data", is a name of a variable. It is an arbitrary name that is only slightly limited - languages have "keywords", so many of those keywords are forbidden to use by user-named items.
The = sign is known as an assignment operator. It takes the expression on the right and copies it to the object on the left.
Finally, ReturnMinusOne(5) is a hypothetical function call. and each language defines its functions differently. What you can know about it is its name and that it takes one argument. Arguments are data that is passed to the function to be used, take for example this function in PHP:
function ReturnMinusOne($num) { return $num - 1; }
Or say, Python:
def ReturnMinusOne(num): return num - 1
num here is a nickname for the first argument passed to the function. Theoretically you could have five, twenty, or a hundred arguments. You can call functions in most languages by writing their name, and in brackets typing their arguments (if any).
So, for example I want to take your name, Chocolate-Mint Swirl, and get how many characters are in it.
Function GetLength(ByVal name As String) As Integer Dim count As Integer = 0 For Each c As Char In name count += 1 Next Return count End Function
This might seem complicated, but if you look closely it isn't, actually most of it is just something I didn't get into, which is types.
Every piece of data has some kind of type. For example, your name, "Chocolate-Mint Swirl", is a string. What is a string? As it implies, it is literally a string of the type char. Just char, char, char, char, together, which forms this new type.
But what is a char? A char is any single character. C, h, o, c, o, etc, each is a char. However note that it can also be many others, such as a digit.
Now an integer is a whole number. Say, 1935, that would be an integer. It could also be a -1935, but never 1935.3, or 1935.9 or 1935.0. That would be a float.
So now that we have types out of the way, let's see the function:
Let's look at the first line:
Function GetLength(ByVal name As String) As Integer
The first word, 'Function', is a keyword. It means that the next word is going to be the name of the function.
'GetLength' is the function name, and must be immediately succeeded by (), following 'As (Type)'.
'As (Type)', say, 'As Integer', or 'As String', it means that this function returns a variable of that type. It is necessary in many languages to declare what type does a function return, what type does a variable hold, etc.
Dim count As Integer = 0
Now, 'Dim' is another keyword, consider it like 'function' except it declares a variable. A variable, as you may remember, is just a container for some data. So the variable count is declared by dim As an Integer that is assigned 0.
For Each c As Char In name count += 1 Next
Now this is an interesting block. This is called a loop - what it does is repeat itself over and over until a certain condition is meant. This one is known as a for each loop, and it will repeat over a container until it went over everything.
The first word, 'For Each' is the key word here that declares the start of a for each loop. The next part,c As Char is a temporary variable containing a single character, and I'll get to it in a bit. In is another keyword, which means the next word (name) is the variable you want to loop over.
So, since a string is composed of many chars, we can loop over each individual char! Cool right? Each time a variable loops over itself, it is known as an iteration. Each iteration of the for each loop assigns (=) the next char in Name as the new C. So, if we have this for example:
For Each c As Char In 'HELLO' count += 1 Next
It is really just:
Dim c As Char = 'H' count += 1 Dim c As Char = 'E' count += 1 Dim c as Char = 'L' count += 1 Dim c as Char = 'L' count += 1 Dim c as Char = 'O'
That's really it! You'll note that we don't actually do anything in 'c'. It's simply a requirement of the for each loop to assign it to something, but in this case we don't really care about it.
You end the For Each loop with the key word 'Next', and finally tell the program that the end of the function is here with the key word 'End Function'
Questions?
|