Game Development Reference
In-Depth Information
func getGasPrices() ->(Double, Double, Double) {
return (3.50, 3.73, 3.87)
}
var prices = getGasPrices()
println(prices)
Take a look at the previous snippet. You first define a function named
getGasPrices() that takes no parameters and returns three Double values. Follow-
ing the function, you are invoking it and storing the results in a variable named prices ,
and finally you are printing that value. To see the tuple value, use this code to replace the
body of main.swift and run it again. The output will look like the following:
(3.5, 3.73, 3.87)
This might look a little strange, but this is what a tuple looks like. If you want to reference
each of the values in the tuple, you can do so by using its numeric position in the tuple.
println(prices.0)
println(prices.1)
println(prices.2)
Replace println() in the previous snippet and rerun it. Now you will see each value
printed on a different line. This seems OK, but it would be much easier to reference each
value using a unique name as opposed to keeping up with its position in the tuple. You can
do this by naming each of the returned values.
func getGasPrices() ->(unleaded : Double, premium : Double,
diesel : Double) {
return (3.50, 3.73, 3.87)
}
Now you can invoke the function and reference the results as follows:
var prices = getGasPrices()
println(prices.unleaded)
println(prices.premium)
println(prices.diesel)
Search WWH ::




Custom Search