Functions (part 1)
So I am now on to the next course! This one is all to do with functions. These I remember fairly well from before but, as I've found, it's not so much the concept but the application of it that I find trickier. We'll see how it goes!
What is a Function?
Pasan explains this through the example of making coffee - a series of steps for a very specific task. When needing coffee, the sae series of steps are carried out!
DRY - Don't Repeat Yourself!
So, rather than going through the steps of something e.g. making coffee every time, we can just call up the function of making coffee. A coffee maker carries out all of these steps so you don't actually have to do each one.
It is a 'self contained block of code that carries out a specific task'.
Functions in Code
What is a Function?
Pasan explains this through the example of making coffee - a series of steps for a very specific task. When needing coffee, the sae series of steps are carried out!
DRY - Don't Repeat Yourself!
So, rather than going through the steps of something e.g. making coffee every time, we can just call up the function of making coffee. A coffee maker carries out all of these steps so you don't actually have to do each one.
It is a 'self contained block of code that carries out a specific task'.
Functions in Code
let length = 12
let width = 10
let area = length * width
I think the point that Pasan is going to make here is that it is cumbersome to keep changing values or doing this each time we want to find out the area of the room. So rather than doing this over and over, a function can be created for this.
func area () {
let length = 12
let width = 10
let areaOfRoom = length * width
print(areaOfRoom)
}
This is much more efficient and useful! Functions 'encapsulate' all of this much more effectively than creating stand alone constants and variables. The next video is about what if the length and width changed? Well, I think that's where parameters come into play...
Function Parameters
I was right - woohoo! So the idea here will be that you can have parameters to put in your own numbers - or inputs. Then the code inside the function will be more general.
func newArea(length: Int, width: Int) -> Int {
return length * width
}
newArea(length: 4, width: 3)
That was done without prompting or help - good stuff! So the inputs are known as parameters. What we have now are two 'arguments'. The function above is completely reusable as you can apply it for any numbers. It is a series of instructions to carry out a specific task, which is self-contained!
Return Types
Pasan says that an App is a combination of functions basically. Whoops - I've already done this! Not a bad thing - in the code above, I automatically went ahead and created a return value, with the arrow and type in the first line, rather than printing the area.
Print - this is a function provided by Swift!
You can create a constant which uses the function and stores the area.
let area1 = newArea(length: 5, width: 7)
Below is an example of a function with void - where nothing is returned...not sure why at the moment!
func someFunction() -> Void {
}
All code challenges and quizzes done! Functions seem familiar, which is great. Actually, to explore these even more, I've created another function just as my own personal challenge...
func stringCount(name: String) -> Int {
return name.count
}
stringCount(name: "Simon")
Actually surprisingly easy! No problems! Another one...
func lannisterDetector(firstName: String, surname: String) -> String {
if surname == "Lannister" {
return "This is a Lannister"
} else {
return "This is not a Lannister. Or check you have spelt the surname correctly and used a capital L!"
}
}
lannisterDetector(firstName: "Tyrion", surname: "Lannister")
Slightly more complex but still simple enough! All good!
So the next series of videos are more about functions - applying them in a context most likely. I think there will be something about overrides, different types of parameters etc. I'm really enjoying this and feel like I can be creative and be logical at the same time! A bit each day has helped me make sense of it - not too much in one go!
Comments
Post a Comment