Ray Wenderlich Functions and Types Course - Part 2

Back on it! Two entries is the plan today - half an hour now and then again later - so as one blog. Let's go!

Start Time - 13:01

Advanced Parameters

Variadic parameters -

So this can be an unspecified number of parameters. E.g. in the print function - could be one or more things to print!

Here is an example -

func getBestGrade(for grades: Int...) -> Int {
    grades.max() ?? 0
    
}


getBestGrade(for: 4, 7, 1, 8, 19, 3)

This is different to what I assumed. I thought it would be an array. 

Inout parameters

func incrementAndPrint(value: Int) {
    value += 1
    print(value)
}

Now the issue here is that the value parameter is a CONSTANT by default. It is immutable! So to fix that we need the inout keyword...

func incrementAndPrint(_ value: inout Int) {
    value += 1
    print(value)
}

OK a couple of things here. It only works on variables, not just whole numbers. The ampersand is needed. 


var score = 1
incrementAndPrint(&score)

Challenge!


Done! 2 and 3 fine too. 

Functions as Parameters

func add(number1: Int, number2: Int) -> Int {
    number1 + number2
}

var function = add

function(4, 2)

func subtract(number1: Int, number2: Int) -> Int {
    number1 - number2
}

function = subtract
function(4, 2)


So we didn't need the parameters after assigning a function to a variable. 

Cool!

func printResult(_ operate: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    let result = operate(a, b)
    print(result)
}

printResult(add, 5, 3)


So functions are a compound type so it is their SIGNATURE that is passed in as a parameter, rather than their type, as it is a mixture of types. That makes sense now. 

Any other functions of the same type signature can also be used! You can use in built ones e.g. * etc. 

printResult(*, 4, 3)

These are called higher order functions.

Type alias

This is awesome! Have used it before but as a reminder - it is great for using instead of signatures. Makes the code less verbose. 


typealias Operate = (Int, Int) -> Int



Perfect time to stop - end of the first chapter! Later I will tackle chapter 2 - closures.

Paused at 13:37 (36 minutes so far)

Restart at 20:54

Chapter 2 - Closures

So I have used these before. I've always found them complicated so a great opportunity to get used to these!

When you store a function in a variable, that variable becomes a closure. Ahhh! 

Closure types are the same as function types. 

Something I've never realised before is that closures do NOT have argument labels. Parameter names are only used in the closure body. If we try to add argument labels (e.g. to the left of the parameter name) then an error comes up. 

You can't use default values either.

A general rule - if a function already exists, use what is in built rather than create a new one. 

Challenge!

OK, think I've done it!

I'm starting to get used to this. The signature can be kept general e.g. in the below - 

typealias Operate = (Double, Double) -> Double

func printResult(_ operate: Operate, _ a: Double, _ b: Double) {
  let result = operate(a, b)
  print(result)
}

As long as two doubles are the input parameters and a double is returned then you can do whatever you want with them!

Finish Time - 21:18 (24 minutes; 1 hour total!)

So I made a neat hour there. Really useful stuff from Catie. Closures are starting to make more sense - they are essentially a signature, like a function but the features are not the same - no argument labels, can be used in-line with code, on declaration no parameter names at all...they are making more sense. 

Summary -

  • Variadic parameters use the ... and for unspecified numbers. Better than using an array for functionality - what you can actually do with the values
  • Inout are for if the parameter value changes. It makes them mutable and uses the ampersand (&) when used
  • Functions can be assigned to values - when they are they are technically closures and don't need parameter names
  • Functions are a compound type as they have their own signatures
  • Functions can be passed in as parameters, as long as the signature is the same (higher order functions)
  • Type alias is a great way to call a signature a simple keyword, rather than using the verbose, complex signature each time
  • Closures have key differences to functions - no argument labels, no default values, no parameter names when they are used and can be used in-line in code


Cool! Next time, more on closures!


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)