App Development with swift question

Hey Swift forums I have been having trouble with an exercise on the book App development with swift book. With unit 2 section 2 on one of the exercises. Ive been getting some errors... I have checked through the book and google and cannot find a fix for the problem. Thank you for any help, I figured I can check here before searching YouTube.



Exercise - Parameters and Argument Labels

Write a new introduction function called introduction. It should take two String parameters, name and home, and one Int parameter, age. The function should print a brief introduction. I.e. if "Mary," "California," and 32 were passed into the function, it might print "Mary, 32, is from California." Call the function and observe the printout.

func introduction() {
    
    name; String() = "Daniel";
    String; home = "Richfield";
    Int(); = 19;
}
print introduction


What you have written isn't really Swift code, it's something else entirely :stuck_out_tongue:

Lets break it down, first point is: "Write a new function called introduction. It should take two String parameters, name and home, and one Int parameter, age."

func introduction(name: String, home: String, age: Int) {
    
}

Next: The function should print a brief introduction I.e if "Mary", "California" and 32 were passed into the function, it might print "Mary, 32, is from California."

func introduction(name: String, home: String, age: Int) {
    print("\(name), \(age), is from \(home)")
}

Finally: Call the function and observe the printout

func introduction(name: String, home: String, age: Int) {
    print("\(name), \(age), is from \(home)")
}

introduction(name: "fiberly", home: "Swift Forums", age: 100)
1 Like

thanks for the help Buckley brined perfectly!

Of course, if you'd looked at the Swift language reference yourself, you would have seen what was wrong. The best way to learn anything is to study it yourself, not ask others to provide the answers

3 Likes

Hey Joanna, I took your advice and unfortunately didn't want to look this up online. Because I figured if it were the 80s I couldn't or would not be able to look it up. But I went back and began reading the references for the swift language. But if you the time I have a few question, if you wouldn't mind answering them for me. regarding the post above. If not

It's cool if you do not want to, you can just ignore this below.

  1. Why are some arguments in the function not used when it is returned?
  2. How can I know which values are the appropriate values?

I'm sorry but, what you are missing here is not knowledge of Swift, it is basic analysis and logic skills.

Read the instructions, one at a time.

Yes you have correctly declared a function called pacing but you haven't yet created the second function calculatePace, which, according to the instructions, it should call twice ; this is intended to teach you how to factor out repeated code into a reusable function.

This is a matter of simple mathematics. How is a pace or speed calculated? Create the calculatePace function to do this calculation and return the result.

Hint : the result could be in miles per hour, or any other convenient unit

The question asks you to calculate the current speed and the speed needed to reach the total distance in the goal time; then to compare them and return an appropriate message.

Okay so again respond only if you want to, I appreciate the help. Also I have more questions, I tried what you said and branched off and googled stuff found other ways and still I could not figure it out. I tried! I tried nesting functions, I don't understand how I can compare these two numbers when they are in functions. I was able to get the above function running smoothly. But the bottom one is where I run into problems.

Hi @fiberly. Don't worry if you're getting stuck; there's a lot of concepts when you're starting programming or a new language and it can be hard to piece them all together at first.

I tend to like to work backwards from a solution, so let's do that:

func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String {
    let actualTime = calculatePace(currentDistance: currentDistance, totalDistance: totalDistance, currentTime: currentTime)
    if actualTime <= goalTime {
        return "Keep it up!"
    } else {
        return "You've got to push it just a bit harder!"
    }
}

The first thing we do in this function is call our previous calculatePace function and assign its return value to a variable (which will be of type Double). To call the calculatePace function, we can pass through the arguments we received to pacing rather than using hardcoded numbers; we can use variables wherever we'd use hardcoded values, and in fact those hardcoded numbers are being converted to variables when you call calculatePace(currentDistance: 1.3, ...).

Then, once we have the actual time taken stored in our actualTime variable, we can compare it with the goalTime we had passed in. Depending on whether we've beaten our goal, we can return either of the two Strings.

2 Likes

I was hoping not to post the actual code, in order to force you to work it out yourself but, since @Torust has already done that, let me give you my version, on the premise that slightly different explanations can sometimes clarify better than one.

I will start by asking why are you nesting a "do nothing" calculatedPace function inside the pacing function? What you should be doing is calling the calculatePace function you already wrote above.

But then you should also correct the logic in that :

func calculatePace(currentDistance: Double, totalDistance: Double, currentTime: Double) -> Double
{
  return currentTime * (totalDistance / currentDistance)
}

(using parentheses around the ratio division helps to make the operator precedence more obvious)

Then you need to call that function from the pacing function :

func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String
{
  let currentArrivalTime = calculatePace(currentDistance: currentDistance, totalDistance: totalDistance, currentTime: currentTime)
  
  if currentArrivalTime > goalTime
  {
    return "You've got to push it just a bit harder!"
  }
  else
  {
    return "Keep it up!"
  }
}

Notice the use of a variable to hold the result of the function, so that you can then compare it to the goalTime parameter.

Finally, you need to call the pacing function and, once again, you should assign the result to a variable, so that you can then pass to something else, without having to call the function again

let message = pacing(currentDistance: 2.4, totalDistance: 4.0, currentTime: 60, goalTime: 120)

print(message)

Just out of interest, there is another way of organising the two functions, just to show the principle of separating functions is more flexible than just the example given in the lab

  func calculatePace(distance: Double, time: Double) -> Double
  {
    return distance / (time / 60) // assumes time is minutes and speed returned is mph
  }
  
  func pacing(currentDistance: Double, totalDistance: Double, currentTime: Double, goalTime: Double) -> String
  {
    let currentSpeed = calculatePace(distance: currentDistance, time: currentTime)
    
    let goalSpeed = calculatePace(distance: totalDistance, time: goalTime)
    
    return currentSpeed < goalSpeed ? "You've got to push it just a bit harder!" : "Keep it up!"
  }
1 Like

thank you, the very lest set of code you posted doesn't run for playgrounds for me :confused: but I seriously appreciate the help because I was stuck on this for a few months now.....