Functions (part 2)
Continuing with functions! My plan in the next hour is to have completed this Functions course. That means there is only one more full one, then I'll be back to the one that I was working on the last time I gave Swift a major push! Anyway, the next few videos are the more fiddly bits about Functions...
Naming Conventions
Pasan starts by an English grammar link - the use of prepositions - what connect the nouns. They also occur in prepositional phrase e.g. along the river, next to the tree etc.
Applying this to functions, we want functions to read as grammatically correct phrases - in the code. Pasan explains that these are confusing but the convention of using these - the naming conventions - is something to get used to.
Argument Labels
Naming Conventions
Pasan starts by an English grammar link - the use of prepositions - what connect the nouns. They also occur in prepositional phrase e.g. along the river, next to the tree etc.
Applying this to functions, we want functions to read as grammatically correct phrases - in the code. Pasan explains that these are confusing but the convention of using these - the naming conventions - is something to get used to.
Argument Labels
func remove(havingValue: String) {
print(havingValue)
}
In this example, it's not very clear in terms of the parameters. So you can have an external and a local name. By adding a keyword after 'havingValue' to be the local name - to be used within the code. The first keyword (outside one) will be the external one.
The terms 'local' and 'external' make sense - the local ones are for the coder, but the external ones are for the person using the code, so that they are readable and make sense.
func getRemainder(value a: Int, divisor b: Int) -> Int {
return a % b
}
The above was the code in the challenge - no problems!
let result = getRemainder(value: 10, divisor: 3)
Same with the above - I'm getting used to creating constants which use the function.
Default Values
I believe that this will be putting in an input with a set value.
func carpetCost(havingArea area: Int, carpetColour colour: String) -> Int {
var price = 0
switch colour {
case "grey":
price = area * 1
case "tan":
price = area * 2
case "blue":
price = area * 4
default:
price = 0
}
return price
}
That's cool - using a switch statement within a function. Pasan then makes some tweaks to this, so that a previous function is used to calculate the area, so a length and width can be put in. For the default value, you can put in e.g. the string in the parameters, which means when calling the function, you don't need to type this in - it has a default!
Return Complex Values
Well this is all about returning multiple values - in this case tuples. This is cool! You have to put the tuple values within brackets, and ensure that what is returned matches up to this.
newCarpet.0
newCarpet.1
The constant I've created can then have the values accessed but it's not very readable. So parameters are needed once more! That is powerful stuff. The index values of .0 and .1 can still be used, but the parameter labels are much more readable and logical.
Function Scope
func arrayModifier(array: [Int]) {
var arrayOfInts = array
arrayOfInts.append(5)
var secondArray = arrayOfInts
}
var arrayOfInts = [1, 2, 3, 4]
arrayModifier(array: arrayOfInts)
This is rather confusing, but the point here is about variable scope. Because 'secondArray' was created within the body of the function, it doesn't actually exist anywhere else! So I couldn't start using that outside the function essentially. If a value is created outside of the code it is global - that means that it could be used within the function.
Another aspect to this is using the underscore before the parameter name, so that this does not come out externally. I remember that before and is pretty useful.
func coordinates (for location: String) -> (Double, Double) {
switch location {
case "Eiffel Tower":
return (48.8582, 2.2945)
case "Great Pyramid":
return (29.9792, 31.1344)
case "Sydney Opera House":
return (33.8587, 151.2140)
default:
return (0, 0)
}
}
This was the challenge and I did it! I NEARLY looked at the comments for help but didn't! Very proud.
So that's all functions completed - in two blogs and around two hours! Next time, it's all about 'object oriented' Swift. This looks a bit more complex and will take most likely four separate blogs. But it's all good! I feel like I am confident with variables, constants, loops, conditionals and functions and have had some good practice along the way.
Comments
Post a Comment