UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 7 : Returning values from functions question -specifically the dice roll function

Forums > 100 Days of SwiftUI

Hi everyone, I am pretty new so thank you for any help you can provide.

I've included my notes, since I just take notes in the xcode playground as I work and then export it to pdf for future reference.

My question is about the dice roll function example.

func rollDice() -> Int { return Int.random(in: 1...6) }

let result = rollDice() print(result)

Each time I ask it to print(result) it generates the same number as the last time. It doesn't seem to replace the variable. So, how am I misunderstanding this function? It creates a func called rollDice, which must be an int, which returns to "rollDice" a random number 1 through 6 into the variable rollDice. If that number can never change, how is this a diceRolling function that returns random numbers. I feel like I really missing something. Like why did he then make a variable "result" equal a variable that was already returned?

My intial code notes

//here we can make a dice roll a function

func rollDice() -> Int {

//what this -> means is that it's a function that recieves data and returns something. So if you want to return your own data you have to make the -> telling it the type of data you want to send back. //this will send back as an interger, it must because we told it that is the type

return Int.random(in: 1...6)

//the actual value is sent back with the return fuction. you use th return key word in the body to return what you want. so here is where its returing a value to "rollDice function. So basically rollDice will store whatever randomInt the computer chooses in the variable rollDice //now we can use roll dice in all kinds of places and from this code they will all use a D6 autmoatically and generate random rolls. }

let result = rollDice() print(result) print(result) print(result) print(result) print(result)

//i am confused here. Each time I type in print(result) shouldn't I get a new randomInt?

3      

hi,

call the function 5 times; don't call the function once and report the result five times.

try this instead:

print(rollDice())
print(rollDice())
print(rollDice())
print(rollDice())
print(rollDice())

hope that helps,

DMG

4      

thank you! I was so tired i had to stop for the day. That helps a lot.

3      

Congratulations on solving your question about returning values from functions!

3      

It seems like you're having a misunderstanding about the behavior of the dice roll function and the variable assignment in your code.

The function rollDice() is designed to return a random integer between 1 and 6 using Int.random(in: 1...6). Every time you call this function, it generates a new random number within that range.

In your initial code, you correctly defined the rollDice() function and used it to assign a value to the result variable with let result = rollDice(). This means that result will store the value returned by the rollDice() function at that moment.

However, in your subsequent code, you're repeatedly printing the same result value multiple times with print(result). This doesn't invoke the rollDice() function again; it just prints the value stored in result multiple times. That's why you're seeing the same number being printed repeatedly.

If you want to generate new random numbers, you need to call the rollDice() function each time before printing. Here's an updated version of your code:

swift Copy code func rollDice() -> Int { return Int.random(in: 1...6) }

let result1 = rollDice() let result2 = rollDice() let result3 = rollDice() let result4 = rollDice() let result5 = rollDice()

print(result1) print(result2) print(result3) print(result4) print(result5) In this updated code, each call to rollDice() will generate a new random number, and you will see different values being printed for each resultX variable.

3      

Hello and welcome! It's great that you're using Xcode Playground for your work. Your dice roll function looks good; it generates a random integer between 1 and 6. When you call rollDice(), it returns a result, which you then print. This code snippet demonstrates a simple dice roll simulation and is an excellent starting point for more complex applications involving random number generation. If you have any specific questions or need further assistance with this or any other coding topic, feel free to ask!

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.