Ray Wenderlich Functions and Types Course - Part 1

No need to go into the obvious lament of how long it has been! Let's just say that it's a fresh new year and new start for 2020! The mini project was great - learned a lot of skills in practice. Now it's time to look at rw.com and decide what to focus on next....

Start Time - 20:34

Right here we go functions and types! It is recommended for what to do next (other than the awful to do list project, which I abandoned). Just approx 30 minutes today.

Introduction

We will start with what we already know about functions, then go on to more complex use of them. Then onto closures - yes! I need that badly! I really want to use the closure features a lot, lot more.
Then enumeration - cool.
Next onto properties and methods.
Finally, classes and protocols.

All on Xcode playgrounds. Get the beginner playground and work alongside Catie. Challenges along the way, cool.

This is cool - I feel like I need to get back into the technical stuff rather than ploughing on with projects that I don't know ALL of the code of.

Review Functions

Brief recap. Argument labels go before the parameter names. You can use underscores in the place....yes all of this is familiar. Good!

Challenge

Great, onto one already!



func stringCombiner(firstName: String, secondName: String = "Gonet") -> String {
    
    return firstName + " " + secondName

}

Easy! What's cool is that when calling the function you have the option of doing it with a different secondName - the default value.

stringCombiner(firstName: "Josh")


stringCombiner(firstName: "Steve", secondName: "Smith")


Overloading

Creating different functions with the same name. Each one must have a difference in parameters or return types. 

Catie recommends doing default values instead of too much overloading. Similar to what I noticed in the challenge. 

This is cool - did it with little support - 

func getPassStatus(grades: [Int]) -> Bool {
    
    var total = 0
    for grade in grades {
        total += grade
    }
    let average = total / grades.count
    return average >= passingGrade
    
}

And we have the stride function - multiple parameters!

for i in stride(from: 10, to: 2, by: -2) {
    print(i)
}

for i in stride(from: 10, through: 2, by: -2) {
    print(i)
}

Another option is return types - you can have the same function name but a different type that is being returned. You would need to declare the type specifically if attaching it to a value. You lose type inference here. 

Summary

  • Functions have various differences - with types, parameters, code inside etc.
  • You can specify default values for one or more parameter - this creates alternative functions on the drop down
  • Overloading is using the same function name but changing an aspect e.g. return type, parameter numbers etc. 

Finish Time 21:08 (34 minutes)

Nice! A good little reintroduction. When I have more time tomorrow or Friday, I will set out some goals and review where I am and structure what I want to and will achieve. Cool!



Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)