Pure Programmer
Blue Matrix


Cluster Map

Functions

L1

This page is under construction. Please come back later.

Functions1.swift
#!/usr/bin/env swift;
import Foundation

func printHello() -> Void {
	print("Hello, world!")
}

func main() -> Void {
	printHello()
	exit(EXIT_SUCCESS)
}
main()

Output
$ swiftc Functions1.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
Functions2.swift
#!/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
$ swiftc Functions2.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
Functions3.swift
#!/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
$ swiftc Functions3.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
Functions4.swift
#!/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
$ swiftc Functions4.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
Functions5.swift
#!/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
Lists6.swift
#!/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
$ swiftc Lists6.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
Maps3.swift
#!/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
$ swiftc Maps3.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
Tuples3.swift
#!/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
$ swiftc Tuples3.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
swift

Questions

Projects

More ★'s indicate higher difficulty level.

References