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?