Best way to pass info between functions? Parameter vs class?

Beginner with another silly question - sorry!

I have two functions and I am trying to use the return values of function A as the content for my notifications in function B. I can't make this a global variable, so I think I should use a parameter but I have no idea how that would work (or should create a huge class instead and make these functions all methods in the same class?)

Function A:

 func generateRandomSentence() {
    let randomSentence = myData.removeLast() //from my struct
    definitionLabel.text = randomSentence.definition
     wordLabel.text = randomSentence.wordCount         

Function B

func createNotification() { 
    let content = UNMutableNotificationContent()
    content.title = randomSetence.definition
    content.body = randomSentence.wordCount // both don't work since it cannot access this from the other function
1 Like

A few things:

  • generateRandomSentence() doesn't actually generate a random sentence, it just fetches an existing sentence (from myData), and applies to some labels. Those are two different responsibilities, and I would split them. You should rename the parts appropriately, perhaps retrieveRandomSentence() and updateLabels(with:)
  • You already have some kind of Sentence type, with a definition and wordCount. You should make retrieveRandomSentence() return a Sentence
  • Your createNotification is named too generally. It doesn't just make a notification, it makes a notification for a given Sentence
  • For that to work, you need to actually provide a Sentence, via parameter.

Something like this:

func fetchRandomSentence() -> Sentence {
    return myData.removeLast()

func updateLabels(with sentence: Sentence) {
    definitionLabel.text = sentence.definition
    wordLabel.text = sentence.wordCount       

func createNotificationContent(for sentence: Sentence) -> UNMutableNotificationContent { 
    let content = UNMutableNotificationContent()
    content.title = sentence.definition
    content.body = sentence.wordCount
    return content

// Wherever you happen to call these:

func doItAll() {
    let randomSentence = fetchRandomSentence()
    updateLabels(with: randomSentence)
    let notificationContent = createNotificationContent(for: randomSentence)
    doSomething(with: notificationContent)

Thank you Alexander for your detailed response!

One thing: you said

You already have some kind of Sentence type

I am not quite sure what that means - when I tried func fetchRandomSentence() -> Sentence it said "cannot find type Sentence in scope". Is this something I need to create like a struct?

Sorry if my questions are basic I am still trying to make sense of functions with parameters and how to fully utilise them

1 Like

You'll see this happens a lot when we use Somethings as a placeholder. It usually means that there's a type there you ought to implement, or that the exact type doesn't matter. In this case, you can take a look at the fetchRandomSentence:

func fetchRandomSentence() -> Sentence {
    return myData.removeLast()

This means that Sentence is whatever myData.removeLast() returns. You never tell us what myData is (based on another post, probably [String]), and the doesn't matter. We don't much care what it is. The same advice applies regardless of whether Sentence is Int, String and what-have-you.

Also, given that typealias exists, you can make Sentence refers back to existing types:

typealias Sentence = String

What's the type of myData and randomSentence?

I assumed you already have some type which defines the definition and wordCount properties. I called this type Sentence.


Hey Alexander,

I still struggle with the terminology but now I understand what you mean. The definition and wordcount come from my MyData struct (big M) and myData (little m) was an instance? I create from it. Now I used MyData it works and I am starting to slowly understand how to pass parameters - it is difficult!!!

Hi Lantua! Nice to hear from you again, and thank you for helping me again! I just finished reading about typealias now, I understand things better so thank you!!

You call removeLast() on myData, so most likely have an array of multiple MyData instances, and its type is [MyData] (a.k.a. Array<MyData>). You can verify in Xcode by option+clicking myData, and it'll show you its data type.

You should improve the name of MyData. It's fine for a place-holder in a simple demo (akin to the famous foo, bar, baz), but it's not at all descriptive of the intent of that struct. Technically, you won't find a single thing on a computer system that isn't data, in one form or another. Ints, Strings, movies, spreadsheets, heck, even programs and compilers, are all just data™.

A better name might be Sentence (after all, you have a function named retrieveRandomSentence(), you would think it returns a Sentence and not a HamBurger. However, that doesn't seem quite right either, because it has a definition field, and sentences don't have definitions. I would think it's just a Word, like from a dictionary, then having a definition field makes sense, but then wordCount doesn't really make much sense. If you give more context on your problem domain, perhaps we can help you come up with a better name, but ultimately its up to you to pick something that's most useful and descriptive.

Never forget that the audience of your code isn't just the compiler, it's other people reading it. So your identifiers should go beyond the minimum necessary (like the way physicists always use a ton of single-letter variable names), and be useful in conveying information to readers who aren't already familiar with your code. It's important to note that you yourself will be one of those readers who aren't familiar with the code, because you'll forget everything that's "obvious" and "in your head" today after say, 3-6 months.


you would think it returns a Sentence and not a HamBurger .

hahahahah!!! :)) Yes you are correct - the naming is bad. I originally started with it being called VocabData (which instead of sentence definitions and word counts, it gave me Chinese words and definitions) – but things got too complicated for me.

This is my first project and a good practice project for me because I am coming across so many new things that I have to learn by asking and doing it myself (much better than just following a tutorial). This community has been wonderful, so thank you!!

1 Like
Terms of Service

Privacy Policy

Cookie Policy