100 Days of SwiftUI

Day 7

2026-04-24

As I prepped my notes page, I realized I’m probably going to be doing stuff I’ve never done in programming before. Not that I’ve ever been a programmer really, but I have dabbled here and there.

Let’s see how it goes!

Functions, part 1

  • Keyword func which I will always want to spell with a k
  • Need to explicitly spell out the parameter in the call (unlike, say, Excel, which uses comma-location dependence)
  • Parameters are part of the function definition, arguments are part of the function call. They are linked by the parameter/argument name. But “it’s not worth really thinking about.”
  • Order is important! (Unlike in R)

Returning values

Single

  • If you have a function, you might want to get a response back (what did the function do?)
  • Ooh, in class practice!

do two strings contain the same letters, regardless of their order? This function should accept two string parameters, and return true if their letters are the same – so, “abc” and “cab” should return true because they both contain one “a”, one “b”, and one “c”.

func compareStrings(string1: String, string2: String) -> Bool {
    if(string1.sorted() == string2.sorted()) {
        return true
    } else {
        return false
    }
}

My answer is different from his, of course. But then he structured it for pedagogical purposes

  • You don’t have to include the return keyword if there’s only one line of code. It seems like a confusing shortcut to me. Why would you bother leaving it out if you’re going to be using it sometimes and sometimes not?
  • you can also just call return to exit a function that doesn’t have an expected return value
  • Expression vs Statement. It’s not exactly “one line or many lines” but for the moment that’s what I’m going with. That or “expressions can’t create variables inside them”.

Multiple

  • Tuples, like arrays, sets, etc allow you to put multiple values into the return
  • format similar to func myFunc() -> (return1:Type1, return2:Type2)
  • ORDER is important!
  • ok, destructuring is weird.
Puppies!

Parameters

  • functions are distinguished by their parameters (you can have 3 functions with the same name but as long as they have different parameters then it’s valid)
  • ok ok ok puppies! ok ok WTF?? Customizing parameters makes sense but also is SUPER confusing
  • internal parameter name is used in the function, external parameter name is used in the call.
  • oh and you don’t have to use a parameter name at all if you use an underscore _ 🫤 — even within the code body

Quizzes

  • Writing functions — 9/12 — syntax
  • Accepting parameters — 11/12
  • Returning values — 9/12
  • Tuples — 6/6
  • Arrays vs sets vs tuples — 6/6
  • Omitting parameter labels — 10/12
tricksy!