Skip to content

Instantly share code, notes, and snippets.

@deepakg
Last active December 18, 2015 20:40
Show Gist options
  • Save deepakg/77c5c476e80a779da51e to your computer and use it in GitHub Desktop.
Save deepakg/77c5c476e80a779da51e to your computer and use it in GitHub Desktop.
//This is a cheat-sheet of sorts for declaring functions in swift
//you can paste the contents of this gist inside a playground to tinker...
//1. Simple
func HelloWorld() {
print("Hello World")
}
HelloWorld() //Hello World
//2. With a param
func speak(noun: String) {
print("Hello \(noun)")
}
//The first param doesn't need to be named when calling
speak("World") //Hello World
//3. Two parameters
func speak(greeting: String, noun: String) {
print("\(greeting) \(noun)")
}
// The first param still doesn't have to be named, but the second one must be
speak("Namaste", noun: "mars") //Namaste mars
// 4.1 "External names" allow you to expose one name to the caller
// while you use a different name inside the function body
// to refer to the same thing
func speak(greeting: String, to noun: String) {
print("\(greeting) \(noun)")
}
speak("Hello", to: "World") //Hello World
// 4.2 you could (ab)use this to force the first parameter to be named
// by caller
func speak(say greeting: String, to noun: String) {
print("\(greeting) \(noun)")
}
speak(say: "Hello", to: "World") //Hello World
// 5. If you use _ for the external name of a parameter,
// you can allow a caller to omit the name of the parameter
// when calling
// The function below can also be written as
// speak(_ greeting: String, _ noun: String)
// but _ for the first parameter is extranous
func speak(greeting: String, _ noun: String) {
print("\(greeting) \(noun)")
}
speak("Hello", "World") //Hello World
// 6. Params can have default values
func say(greeting: String = "Hello", noun: String = "World") {
print("\(greeting) \(noun)")
}
// Use defaults for both params
say() //Hello World
// Override the first param
say("Goedemorgen") //Goedmorgen World
// Override the second param
say(noun: "Mars") //Hello Mars
// Override both params
say("Goedemorgen", noun: "Mars") //Goedmorgen Mars
// 7. "Variadic" parameters are specified by appending a ... after the type
// They automatically become an array, so noun here is of type [String]
func hello (greeting: String, noun: String...) {
for thing in noun {
print("\(greeting) \(thing)")
}
}
hello("Hello", noun:"Venus", "World", "Mars")
//Hello Venus
//Hello World
//Hello Mars
// 8. By default parameters are constants and cannot be altered inside a functin
// but by Using var when declaring a parameter allows you to use it
// as a variable. However, the scope is limited to the function's body
// i.e. the caller won't see any changes made to the parameter.
func yell (var greeting: String, noun: String) {
greeting = greeting + " " + noun
print(greeting)
}
yell("Hello", noun: "World") //Hello World
// 9. You can use inout parameters to pass values by reference
// any changes made to parameters inside the function body
// will now be visibile beyond the function's scope
func yell(inout greeting: String, inout noun: String) {
greeting = greeting + " " + noun
}
var greeting = "Hello"
var noun = "World"
yell(&greeting, noun: &noun)
print(greeting)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment