Functions

This page is under construction. Please come back later.
#!/usr/bin/env swift;
import Foundation
func printHello() -> Void {
print("Hello, world!")
}
func main() -> Void {
printHello()
exit(EXIT_SUCCESS)
}
main()
Output
Hello, world!
#!/usr/bin/env swift;
import Foundation
func printHello(_ name:String) -> Void {
print("Hello, " + name + "!")
}
func main() -> Void {
printHello("Fred")
printHello("Wilma")
printHello("Barney")
printHello("Betty")
exit(EXIT_SUCCESS)
}
main()
Output
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
#!/usr/bin/env swift;
import Foundation
import Utils
func squared(_ j:Int) -> Int {
return j * j
}
func main() -> Void {
for i:Int in 1...10 {
print(Utils.format("{0:d}^2 = {1:d}", i, squared(i)))
}
exit(EXIT_SUCCESS)
}
main()
Output
1^2 = 1
2^2 = 4
3^2 = 9
4^2 = 16
5^2 = 25
6^2 = 36
7^2 = 49
8^2 = 64
9^2 = 81
10^2 = 100
#!/usr/bin/env swift;
import Foundation
import Utils
func exponentiation(_ base:Double, _ powerArg:Int) -> Double {
var power:Int = powerArg
var result:Double = 1
while power > 0 {
result *= base
power -= 1
}
return result
}
func main() -> Void {
for b:Double in stride(from: 1.1, to: 2.05, by: 0.1) {
for p:Int in 2...10 {
print(Utils.format("{0:f} ^ {1:d} = {2:f}", b, p, exponentiation(b, p)))
}
}
exit(EXIT_SUCCESS)
}
main()
Output
1.100000 ^ 2 = 1.210000
1.100000 ^ 3 = 1.331000
1.100000 ^ 4 = 1.464100
1.100000 ^ 5 = 1.610510
1.100000 ^ 6 = 1.771561
1.100000 ^ 7 = 1.948717
1.100000 ^ 8 = 2.143589
1.100000 ^ 9 = 2.357948
1.100000 ^ 10 = 2.593742
1.200000 ^ 2 = 1.440000
1.200000 ^ 3 = 1.728000
1.200000 ^ 4 = 2.073600
1.200000 ^ 5 = 2.488320
1.200000 ^ 6 = 2.985984
1.200000 ^ 7 = 3.583181
1.200000 ^ 8 = 4.299817
1.200000 ^ 9 = 5.159780
1.200000 ^ 10 = 6.191736
1.300000 ^ 2 = 1.690000
1.300000 ^ 3 = 2.197000
1.300000 ^ 4 = 2.856100
1.300000 ^ 5 = 3.712930
1.300000 ^ 6 = 4.826809
1.300000 ^ 7 = 6.274852
1.300000 ^ 8 = 8.157307
1.300000 ^ 9 = 10.604499
1.300000 ^ 10 = 13.785849
1.400000 ^ 2 = 1.960000
1.400000 ^ 3 = 2.744000
1.400000 ^ 4 = 3.841600
1.400000 ^ 5 = 5.378240
1.400000 ^ 6 = 7.529536
1.400000 ^ 7 = 10.541350
1.400000 ^ 8 = 14.757891
1.400000 ^ 9 = 20.661047
1.400000 ^ 10 = 28.925465
1.500000 ^ 2 = 2.250000
1.500000 ^ 3 = 3.375000
1.500000 ^ 4 = 5.062500
1.500000 ^ 5 = 7.593750
1.500000 ^ 6 = 11.390625
1.500000 ^ 7 = 17.085938
1.500000 ^ 8 = 25.628906
1.500000 ^ 9 = 38.443359
1.500000 ^ 10 = 57.665039
1.600000 ^ 2 = 2.560000
1.600000 ^ 3 = 4.096000
1.600000 ^ 4 = 6.553600
1.600000 ^ 5 = 10.485760
1.600000 ^ 6 = 16.777216
1.600000 ^ 7 = 26.843546
1.600000 ^ 8 = 42.949673
1.600000 ^ 9 = 68.719477
1.600000 ^ 10 = 109.951163
1.700000 ^ 2 = 2.890000
1.700000 ^ 3 = 4.913000
1.700000 ^ 4 = 8.352100
1.700000 ^ 5 = 14.198570
1.700000 ^ 6 = 24.137569
1.700000 ^ 7 = 41.033867
1.700000 ^ 8 = 69.757574
1.700000 ^ 9 = 118.587876
1.700000 ^ 10 = 201.599390
1.800000 ^ 2 = 3.240000
1.800000 ^ 3 = 5.832000
1.800000 ^ 4 = 10.497600
1.800000 ^ 5 = 18.895680
1.800000 ^ 6 = 34.012224
1.800000 ^ 7 = 61.222003
1.800000 ^ 8 = 110.199606
1.800000 ^ 9 = 198.359290
1.800000 ^ 10 = 357.046723
1.900000 ^ 2 = 3.610000
1.900000 ^ 3 = 6.859000
1.900000 ^ 4 = 13.032100
1.900000 ^ 5 = 24.760990
1.900000 ^ 6 = 47.045881
1.900000 ^ 7 = 89.387174
1.900000 ^ 8 = 169.835630
1.900000 ^ 9 = 322.687698
1.900000 ^ 10 = 613.106626
2.000000 ^ 2 = 4.000000
2.000000 ^ 3 = 8.000000
2.000000 ^ 4 = 16.000000
2.000000 ^ 5 = 32.000000
2.000000 ^ 6 = 64.000000
2.000000 ^ 7 = 128.000000
2.000000 ^ 8 = 256.000000
2.000000 ^ 9 = 512.000000
2.000000 ^ 10 = 1024.000000
#!/usr/bin/env swift;
import Foundation
import Utils
func factorial(_ x:Int) -> Int64 {
if x <= 1 {
return 1
}
return x * factorial(x - 1)
}
func main() -> Void {
for x:Int in 1..<10 {
print(Utils.format("{0:d}! = {1:d}", x, factorial(x)))
}
exit(EXIT_SUCCESS)
}
main()
Output
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/swift/examples/output/Functions5.out
#!/usr/bin/env swift;
import Foundation
import Utils
func reverseList(_ x:[String]) -> [String] {
var y:[String] = []
for i:Int in stride(from: x.count - 1, through: 0, by: -1) {
y.append(x[i])
}
return y
}
func main() -> Void {
var names:[String] = ["Fred", "Wilma", "Barney", "Betty"]
for name in names {
print("Hello, " + name + "!")
}
names = reverseList(names)
for name in names {
print("Hello, " + name + "!")
}
print(Utils.listToString(names))
exit(EXIT_SUCCESS)
}
main()
Output
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
Hello, Betty!
Hello, Barney!
Hello, Wilma!
Hello, Fred!
["Betty", "Barney", "Wilma", "Fred"]
#!/usr/bin/env swift;
import Foundation
import Utils
func convertKmtoMiles(_ x:[String: Int]) -> [String: Int] {
var y:[String: Int] = [:]
for planet in x.keys {
y[planet] = Int(Double(x[planet]) * 0.621371 + 0.5)
}
return y
}
func main() -> Void {
var planetDiametersInKm:[String: Int] = [
"Mercury": 4879,
"Venus": 12103,
"Earth": 12756,
"Mars": 6794,
"Jupiter": 142985,
"Saturn": 120534,
"Uranus": 51115,
"Neptune": 49534,
"Pluto": 2374,
"Ceres": 946,
"Eris": 2326,
"Makemake": 1430
]
var planetDiametersInMiles:[String: Int] = convertKmtoMiles(planetDiametersInKm)
for planet in planetDiametersInMiles.keys {
print(planet + " has a diameter of " + String(planetDiametersInMiles[planet]) + " miles")
}
exit(EXIT_SUCCESS)
}
main()
Output
Maps3.swift:8:27: error: value of optional type 'Int?' must be unwrapped to a value of type 'Int'
6 | var y:[String: Int] = [:]
7 | for planet in x.keys {
8 | y[planet] = Int(Double(x[planet]) * 0.621371 + 0.5)
| |- error: value of optional type 'Int?' must be unwrapped to a value of type 'Int'
| |- note: coalesce using '??' to provide a default when the optional value contains 'nil'
| `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
9 | }
10 | return y
Maps3.swift:31:71: error: value of optional type 'Int?' must be unwrapped to a value of type 'Int'
29 | var planetDiametersInMiles:[String: Int] = convertKmtoMiles(planetDiametersInKm)
30 | for planet in planetDiametersInMiles.keys {
31 | print(planet + " has a diameter of " + String(planetDiametersInMiles[planet]) + " miles")
| |- error: value of optional type 'Int?' must be unwrapped to a value of type 'Int'
| |- note: coalesce using '??' to provide a default when the optional value contains 'nil'
| `- note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
32 | }
33 | exit(EXIT_SUCCESS)
#!/usr/bin/env swift;
import Foundation
import Utils
func swap(_ x:(String, Int)) -> (Int, String) {
var y:(Int, String)
y = (x.1, x.0)
return y
}
func main() -> Void {
var pair1 = ("Hello", 5)
var pair2:(Int, String)
pair2 = swap(pair1)
print(Utils.tupleToString(pair1) + " becomes " + Utils.tupleToString(pair2))
exit(EXIT_SUCCESS)
}
main()
Output
Tuples3.swift:12:6: warning: variable 'pair1' was never mutated; consider changing to 'let' constant
10 |
11 | func main() -> Void {
12 | var pair1 = ("Hello", 5)
| `- warning: variable 'pair1' was never mutated; consider changing to 'let' constant
13 | var pair2:(Int, String)
14 | pair2 = swap(pair1)
<"Hello", 5> becomes <5, "Hello">
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Alouette Lyrics
- Bingo Lyrics
- 99 Bottles of Beer Lyrics
- Bubble Sort
- Calculate Euler's Number
- Compute π Function
- Compute π (Monte Carlo with Central Limit Theorem)
- Credit Card Validator
- The Farmer in the Dell (Revisited)
- Fibonacci Numbers
- Football Passer Rating (Revisited)
- GCD
- Insertion Sort
- LCG Pseudo-Random Number Generator
- License Plate Generator (Revisited)
- Merge Sort
- Min/Max Functions
- Old MacDonald Lyrics
- Quicksort
- Renard Numbers with Rounding Function
- Roman Numerals
- Sign Function
- Spreadsheet Columns
- Tower of Hanoi
- Twelve Days of Christmas Lyrics
References
- [[Swift Community]]
- [[Swift Language Guide]]
- [[Swift Language Reference]]
- [[Swift Programming Language]], Apple Inc.
- [[Swift Doc]]
- [[We Heart Swift]]
- [[Swift Cookbook]]
- [[Swift Playground]]
- [[Swift at TutorialsPoint]]
- [[Hacking with Swift]]
Pure Programmer


